home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / MenuScripter 3.1 / Sources / MSAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  167.2 KB  |  6,202 lines  |  [TEXT/MPS ]

  1. /*
  2.     MSAppleEvents.c 
  3.     
  4.     Version 3.1
  5.     
  6.     Copyright © 1995 Apple Computer, Inc., all rights reserved.
  7.     
  8.     MenuScripter by Nigel Humphreys and Jon Lansdell
  9.     AppleEvent to script extensions by Greg Sutton
  10.     
  11.     The AppleEvent handling routines for the MenuScripter example program
  12.     
  13.     This file includes :
  14.     
  15.         a) the code for the AppleEvent initialisation 
  16.         b) the routines for all the Edition Manager events
  17.         c) the Apple® Event Object Support
  18.  
  19. */
  20.  
  21. #include <AppleEvents.h>
  22. #include <Menus.h>
  23. #include <PLStringFuncs.h>
  24. #include <Scrap.h>
  25. #include <LowMem.h>
  26. #include <TextEdit.h>
  27. #include <AEObjects.h>
  28. #include <AEPackObject.h>
  29. #include <AERegistry.h>
  30. #include <ASRegistry.h>
  31. #include <TextUtils.h>
  32. #include "MSGlobals.h"
  33. #include "MSUtils.h"
  34. #include "MSAEUtils.h"
  35. #include "MSWindow.h"
  36. #include "MSFile.h"
  37. #include "MSScript.h"
  38. #include "MSAppleEvents.h"
  39.  
  40. /* these should come from the registry */
  41.         
  42. #define         kAEStartedRecording    'rec1'
  43. #define         kAEStoppedRecording         'rec0'
  44. #define         kAEDontExecute                  0x00002000
  45. #define         errCantWriteSubscriber 10000
  46.  
  47. #define         pContents                 'pcnt' // Borrowed from Quill
  48. #define     cSpot             'cspt'
  49.  
  50. /*
  51.     Text Properties
  52. */
  53.         
  54. #define         pStringWidth            'pwid'
  55.         
  56. /*
  57.     Window Properties - See the Registry for Details
  58. */
  59.         
  60. #define     pPosition                'ppos'
  61. #define         pPageSetup                'PSET' /* One of ours - Not in registry */
  62. #define         pShowBorders            'PBOR' /* Another of ours */
  63. #define     pSelection        'sele' /* changed from pUserSelection */
  64.                 
  65. #define         typeTPrint                'TPNT' /* A raw TPrint record - also one of ours */
  66.  
  67.  
  68. #define     kMenuScripterSuite    'MeSc'
  69. #define     kAEApplyFilter            'ApFi'
  70.  
  71. /*
  72.     Menu properties of our own devising
  73. */
  74.  
  75. #define     pActiveItem       'PACT'
  76.  
  77. /*
  78.     Error Codes
  79. */
  80.         
  81. #define         kAEGenericErr    -1799
  82.  
  83. static long   gBigBrother;
  84. static char  *gTypingBuffer;
  85. static short  gCharsInBuffer;
  86. static AEDesc gTypingTargetObject;
  87.  
  88.  
  89. /*-----------------------------------------------------------------------*/
  90. /**----------                         APPLE EVENT HANDLING                     ---------------**/
  91. /*-----------------------------------------------------------------------*/
  92.  
  93. pascal OSErr GetTHPrintFromDescriptor(const AEDesc *sourceDesc, THPrint *result)
  94.   {
  95.       OSErr   myErr;
  96.         OSErr   ignoreErr;
  97.         Size    ptSize;
  98.         AEDesc  resultDesc;
  99.         
  100.         myErr = AECoerceDesc(sourceDesc,typeTPrint,&resultDesc);
  101.         
  102.         *result = nil;
  103.         
  104.         if (myErr==noErr) 
  105.             {
  106.                 *result = (THPrint)NewHandle(sizeof(TPrint));
  107.                 
  108.                 PrOpen();
  109.                 PrintDefault(*result);
  110.                 
  111.                 HLock((Handle)*result);
  112.     
  113.                 GetRawDataFromDescriptor(&resultDesc,
  114.                                                                  (Ptr)**result,
  115.                                                                  sizeof(TPrint),
  116.                                                                  &ptSize);
  117.                                                                  
  118.                 HUnlock((Handle)*result);
  119.                 
  120.                 if ((ptSize<sizeof(TPrint)) || (PrValidate(*result)))
  121.                     {
  122.                         myErr = errAECoercionFail;
  123.                         DisposHandle((Handle)*result);
  124.                         *result = nil;
  125.                     }
  126.                 
  127.                 PrClose();
  128.             }
  129.         
  130.         if (resultDesc.dataHandle) 
  131.             ignoreErr = AEDisposeDesc(&resultDesc);
  132.             
  133.         return(myErr);
  134.     } /*GetTHPrintFromDescriptor*/
  135.  
  136. /*******************************************************************************/
  137. /*
  138.     Object Accessors - Utility Routines
  139. */
  140.  
  141. #pragma segment ObjectAccessors
  142.  
  143. pascal WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
  144. /* 
  145.     Returns the WindowPtr of the window with title nameStr
  146.     or nil if there is no matching window.
  147. */
  148.     { 
  149.         WindowPtr theWindow;
  150.         Str255    windTitle;
  151.             
  152.         theWindow = (WindowPtr)LMGetWindowList();
  153.         /* 
  154.             iterate through windows - we use WindowList 'cos we could
  155.             have made the window invisible and  we lose it - so we
  156.             can't set it back to visible!!
  157.         */
  158.         while (theWindow)
  159.             {
  160.                 GetWTitle(theWindow, windTitle);
  161.                 if (EqualString(windTitle,
  162.                                 nameStr,
  163.                                                 false,
  164.                                                 true))     /* ignore case, don't ignore diacriticals */
  165.                     return(theWindow);
  166.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  167.             }
  168.         return(theWindow);
  169.     }    /* WindowNameToWindowPtr */
  170.  
  171. pascal WindowPtr GetWindowPtrOfNthWindow(short index)
  172. /* returns a ptr to the window with the given index
  173.   (front window is 1, behind that is 2, etc.).  if
  174.   there's no window with that index (inc. no windows
  175.   at all), returns nil.
  176. */
  177.   {
  178.       WindowPtr theWindow;
  179.         
  180.         theWindow = (WindowPtr)LMGetWindowList();
  181.     
  182.         /* iterate through windows */
  183.         
  184.         while (theWindow)
  185.             {
  186.                 index --;
  187.                 if (index <= 0) 
  188.                     return(theWindow);
  189.                     
  190.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  191.             }
  192.         return(nil);
  193.     }    /* GetWindowPtrOfNthWindow */
  194.     
  195. pascal short CountWindows(void)
  196.     {
  197.         WindowPtr theWindow;
  198.         short     index;
  199.                 
  200.         index = 0;
  201.         theWindow = (WindowPtr)LMGetWindowList();
  202.     
  203.         /* iterate through windows */
  204.         
  205.         while (theWindow)
  206.             {
  207.                 index++;                    
  208.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  209.             }
  210.         
  211.         return(index);
  212.         
  213.     } /*CountWindows*/
  214.     
  215. pascal short ItemForNamedFont(Str255 theName)
  216.     {
  217.       Str255  itemName;
  218.         short   limit;
  219.     
  220.         limit = CountMItems(myMenus[fontM]);
  221.         while (limit>0)
  222.             {
  223.                 GetItem(myMenus[fontM],limit, itemName);
  224.                 if (IUEqualString(theName, itemName)==0) 
  225.                     return(limit);
  226.                 else
  227.                     limit--;
  228.             }
  229.         return(0);
  230.     } /*ItemForNamedFont*/
  231.  
  232. /**-----------------------------------------------------------------------
  233.         Name:             DoOpenApp
  234.         Purpose:        Called on startup, creates a new document.
  235.     -----------------------------------------------------------------------**/
  236.  
  237. #pragma segment Main
  238.  
  239. pascal OSErr DoOpenApp(const AppleEvent *message,const AppleEvent *reply,long refcon)
  240.   {
  241. #pragma unused (reply,refcon, message)
  242.     
  243.       DPtr ourDoc;
  244.     
  245.         /*just create a new document*/
  246.         ourDoc = NewDocument(false);
  247.         
  248.         if (ourDoc)
  249.             {
  250.                 ShowWindow(ourDoc->theWindow);
  251.                 return(noErr);
  252.             }
  253.         else
  254.             return(-108);
  255.     }
  256.  
  257. /**-----------------------------------------------------------------------
  258.         Name:             DoOpenDocument
  259.         Purpose:        Open all the documents passed in the Open AppleEvent.
  260. -----------------------------------------------------------------------**/
  261.  
  262. #pragma segment Main
  263.  
  264. pascal OSErr DoOpenDocument(const AppleEvent *message, const AppleEvent *reply, long refcon)            
  265.   {
  266. #pragma unused (reply, refcon)
  267.  
  268.         long        index;
  269.         long        itemsInList;
  270.         AEKeyword   keywd;
  271.         OSErr       err;
  272.         OSErr       ignoreErr;
  273.         AEDescList  docList;
  274.         long        actSize;
  275.         DescType    typeCode;
  276.         FSSpec      theFSSpec;
  277.         
  278.         /*open the specified documents*/
  279.         
  280.         docList.dataHandle = nil;
  281.         
  282.         err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  283.         
  284.         if (err==noErr)
  285.             err = AECountItems( &docList, &itemsInList) ;
  286.         else
  287.           itemsInList = 0;
  288.             
  289.         for (index = 1; index <= itemsInList; index++)
  290.             if (err==noErr)
  291.                 {
  292.                     err = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  293.                                                          (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize ) ;
  294.                     if (err==noErr)
  295.                       err = OpenOld(theFSSpec);
  296.                 }
  297.     
  298.       if (docList.dataHandle)
  299.             ignoreErr = AEDisposeDesc(&docList);
  300.             
  301.         return(err);
  302.     }
  303.  
  304. /**-----------------------------------------------------------------------
  305.         Name:             MyQuit
  306.         Purpose:        Quit event received- exit the program.
  307.     -----------------------------------------------------------------------**/
  308.  
  309. #pragma segment Main
  310.  
  311. pascal OSErr MyQuit(const AppleEvent *message,const AppleEvent *reply,long refcon)            
  312.     {
  313. #pragma unused (reply,refcon)
  314.     
  315.         DescType saveOpt;
  316.         OSErr    tempErr;
  317.         OSErr    myErr;
  318.         DescType returnedType;
  319.         long     actSize;
  320.         
  321.         saveOpt = kAEAsk; /* the default */
  322.         tempErr = AEGetParamPtr(message,
  323.                                                         keyAESaveOptions,
  324.                                                         typeEnumerated,
  325.                                                         &returnedType,
  326.                                                         (Ptr)&saveOpt,
  327.                                                         sizeof(saveOpt),
  328.                                                         &actSize);
  329.         
  330.         if (saveOpt != kAENo)
  331.             myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  332.  
  333.       if (myErr == noErr)
  334.             DoQuit(saveOpt);
  335.         
  336.         return(myErr);
  337.     }
  338.  
  339. /**-----------------------------------------------------------------------
  340.         Name:             DoAppleEvent
  341.         Purpose:        Process and despatch the AppleEvent
  342.     -----------------------------------------------------------------------**/
  343.  
  344. #pragma segment Main
  345.  
  346. pascal void DoAppleEvent(EventRecord theEvent)
  347.   {
  348.       OSErr err;
  349.         
  350.       /*should check for your own event message types here - if you have any*/
  351.  
  352.         err = AEProcessAppleEvent(&theEvent);
  353.     }
  354.  
  355. /**-----------------------------------------------------------------------
  356.         Name:             MakeSelfAddress
  357.         Purpose:        Builds an AEAddressDesc for the current process
  358.     -----------------------------------------------------------------------**/
  359.     
  360. pascal OSErr MakeSelfAddress(AEAddressDesc *selfAddress)
  361.   {
  362.       ProcessSerialNumber procSerNum;
  363.     
  364.         procSerNum.highLongOfPSN = 0;
  365.         procSerNum.lowLongOfPSN  = kCurrentProcess;
  366.         
  367.         return(AECreateDesc(typeProcessSerialNumber,
  368.                                                 (Ptr)&procSerNum,
  369.                                               sizeof(procSerNum),
  370.                                                 selfAddress));
  371.         
  372.     } /* MakeSelfAddress */
  373.  
  374. /**--------------------------------------------------------------------
  375.     Name : SendAESetObjProp
  376.     Function : Creates a property object from an object,
  377.                          a property type and its data and sends it to
  378.                          the requested address, and cleans up zapping params too
  379.     --------------------------------------------------------------------**/
  380.  
  381. pascal OSErr SendAESetObjProp(AEDesc        *theObj,
  382.                                                             DescType      theProp,
  383.                                                             AEDesc        *theData,
  384.                                                             AEAddressDesc *toWhom)
  385.                                                      
  386.     {
  387.        AEDesc     propObjSpec;
  388.          AppleEvent myAppleEvent;
  389.          AppleEvent defReply;
  390.          OSErr      myErr;
  391.          OSErr      ignoreErr;
  392.          AEDesc     theProperty;
  393.             
  394.         /* create an object spec that represents the property of the given object */
  395.         
  396.         myErr = AECreateDesc(typeType,
  397.                                                  (Ptr)&theProp,
  398.                                                  sizeof(theProp),
  399.                                                  &theProperty);
  400.         if (myErr==noErr)
  401.             myErr = CreateObjSpecifier(cProperty,
  402.                                                                  theObj,
  403.                                                                  formPropertyID,
  404.                                                                  &theProperty,
  405.                                                                  true,
  406.                                                                  &propObjSpec);    
  407.             
  408.         /* create event */
  409.         
  410.         if (myErr==noErr)
  411.             myErr = AECreateAppleEvent(kAECoreSuite,
  412.                                                                  kAESetData,
  413.                                                                  toWhom,
  414.                                                                  0,
  415.                                                                  0,
  416.                                                                  &myAppleEvent);
  417.             
  418.         /* add prop obj spec to the event */
  419.         
  420.         if (myErr==noErr)
  421.             myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &propObjSpec);
  422.         
  423.         /* add prop data to the event */
  424.         
  425.         if (myErr==noErr)
  426.             myErr = AEPutParamDesc(&myAppleEvent,keyAEData, theData);
  427.         
  428.         /* send event */
  429.         
  430.         if (myErr==noErr)
  431.             myErr = AESend(&myAppleEvent,
  432.                                          &defReply,
  433.                                          kAENoReply+kAEAlwaysInteract,
  434.                                          kAENormalPriority,
  435.                                          kAEDefaultTimeout,
  436.                                          nil,
  437.                                          nil);
  438.         
  439.         if (myAppleEvent.dataHandle)
  440.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  441.             
  442.         if (&propObjSpec.dataHandle)
  443.           ignoreErr = AEDisposeDesc(&propObjSpec);
  444.         
  445.         if (theData->dataHandle)
  446.             ignoreErr = AEDisposeDesc(theData);
  447.         
  448.         if (toWhom->dataHandle)
  449.             ignoreErr = AEDisposeDesc(toWhom);
  450.         
  451.         return(myErr);
  452.         
  453.     }    /* SendAESetObjProp */
  454.  
  455. /*----------------------------------------------------------------------------------------------*/
  456. /*
  457.     Private AEObject definitions
  458. */
  459. #pragma segment AECommandHandlers
  460.  
  461. #define typeMyAppl       'BAPP' /* sig of my private token type for the app     - appToken   */
  462. #define typeMyWndw             'BWIN'    /* sig of my private token type for windows     - windowToken   */
  463. #define typeMyText           'BTXT'    /* sig of my private token type for text        - textToken     */
  464. #define typeMyTextProp   'BPRP'    /* sig of my private token type for text properties    - textPropToken */
  465. #define typeMyWindowProp 'WPRP'    /* sig of my private token type for window properties  - windowPropToken */
  466. #define typeMyApplProp   'APRP'    /* sig of my private token type for appl properties    - applPropToken */
  467. #define typeMyMenu       'MTKN' /* sig of my private token type for menus       - menuToken  */
  468. #define typeMyMenuItem   'ITKN' /* sig of my private token type for menus       - menuItemToken  */
  469. #define typeMyMenuProp   'MPRP' /* sig of my private token type for menu properties - menuPropToken  */
  470. #define typeMyItemProp   'IPRP' /* sig of my private token type for menu item properties  - menuItemPropToken  */
  471.     
  472. /* These are entirely private to our app - used only when resolving the object specifier */
  473.     
  474. typedef    ProcessSerialNumber appToken;
  475.     
  476. struct applPropToken{
  477.     appToken tokenApplToken;
  478.     DescType tokenApplProperty;
  479. };
  480.  
  481. typedef struct applPropToken applPropToken;
  482.                                         
  483. typedef    WindowPtr WindowToken;
  484.     
  485. struct windowPropToken{
  486.         WindowToken tokenWindowToken;
  487.         DescType    tokenProperty;
  488.     };
  489.     
  490. typedef struct windowPropToken windowPropToken;
  491.     
  492. struct TextToken{
  493.         WindowPtr tokenWindow;
  494.         short     tokenOffset;
  495.         short     tokenLength;
  496.     };
  497.                     
  498. typedef struct TextToken TextToken;
  499.  
  500. struct textPropToken{
  501.         TextToken propertyTextToken;
  502.         DescType  propertyProperty;
  503.     };
  504.                 
  505. typedef struct textPropToken textPropToken;
  506.  
  507. /* Tokens related to menus */
  508.  
  509. struct MenuToken {
  510.     MenuHandle theTokenMenu;
  511.     short      theTokenID;
  512. };
  513.  
  514. typedef struct MenuToken MenuToken;
  515.  
  516. struct MenuItemToken {
  517.     MenuToken  theMenuToken;
  518.     short      theTokenItem;
  519. };
  520.  
  521. typedef struct MenuItemToken MenuItemToken;
  522.  
  523. struct MenuPropToken {
  524.     MenuToken  theMenuToken;
  525.     DescType   theMenuProp;
  526. };
  527.  
  528. typedef struct MenuPropToken MenuPropToken;
  529.  
  530. struct MenuItemPropToken {
  531.     MenuItemToken  theItemToken;
  532.     DescType       theItemProp;
  533. };
  534.  
  535. typedef struct MenuItemPropToken MenuItemPropToken;
  536.  
  537.  
  538. /*
  539.     Name: GotRequiredParams
  540.     Function: Checks all parameters defined as 'required' have been read
  541. */                            
  542. pascal OSErr GotRequiredParams(const AppleEvent *theAppleEvent)
  543.     {
  544.       OSErr    myErr;
  545.         DescType returnedType;
  546.         Size     actSize;
  547.         
  548.         /* look for the keyMissedKeywordAttr, just to see if it's there */
  549.         
  550.         myErr = AEGetAttributePtr(theAppleEvent,
  551.                                                             keyMissedKeywordAttr,
  552.                                                             typeWildCard,
  553.                                                             &returnedType,
  554.                                                             nil,
  555.                                                             0,
  556.                                                             &actSize);
  557.         
  558.         if (myErr == errAEDescNotFound) 
  559.             return(noErr);            /* attribute not there means we got all req params */
  560.         else 
  561.             if (myErr == noErr)  
  562.                 return(errAEParamMissed);        /* attribute there means missed at least one */
  563.             else 
  564.                 return(myErr);        /* some unexpected arror in looking for the attribute */
  565.     }    /* GotReqiredParams */
  566.  
  567. /**--------------------------------------------------------------------
  568.     Name : SetSelectionOfAppleEventDirectObject
  569.     Function : Resolves the Direct Object into a text token and
  570.                          sets the selection of the specified document to that
  571.                          specified in the direct object.
  572.                          Returns the doc and TEHandle chosen.
  573.     --------------------------------------------------------------------**/
  574.  
  575. pascal OSErr SetSelectionOfAppleEventDirectObject(const AppleEvent *theAppleEvent,
  576.                                                                                                DPtr             *theDocument,
  577.                                                                                                 TEHandle         *theHTE)
  578.     {
  579.         OSErr     myErr;
  580.         DescType  returnedType;
  581.         long      actSize;
  582.         TextToken myTextToken;
  583.         OSErr     paramErr;
  584.         WindowPtr fWin;
  585.                     
  586.         paramErr = AEGetParamPtr( theAppleEvent,
  587.                                                             keyDirectObject,
  588.                                                             typeMyText,
  589.                                                             &returnedType,
  590.                                                       (Ptr)&myTextToken,
  591.                                                             sizeof(myTextToken),
  592.                                                             &actSize);
  593.               
  594.         myErr = GotRequiredParams(theAppleEvent);
  595.         
  596.         /* now let's work on the direct object, if any */
  597.         
  598.         if (paramErr == errAEDescNotFound) 
  599.             {
  600.                 /* no direct object; check we have a window */
  601.                 
  602.                 fWin = FrontWindow();
  603.  
  604.                 if (fWin == nil) 
  605.                     return(-1700); /* Generic Err */
  606.                     
  607.                 *theDocument = DPtrFromWindowPtr(fWin);
  608.                 *theHTE      = (**theDocument).theText;
  609.             }
  610.         
  611.         if (paramErr == noErr) 
  612.             {
  613.                 /* got a text token */
  614.                 
  615.                 *theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  616.                 *theHTE      = (**theDocument).theText;
  617.                 
  618.                 TESetSelect(myTextToken.tokenOffset-1,
  619.                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  620.                                         *theHTE);
  621.                 
  622.             }
  623.             
  624.         if ((paramErr!=noErr) &&
  625.             (paramErr!=errAEDescNotFound))
  626.              {
  627.                  *theDocument = DPtrFromWindowPtr(FrontWindow());
  628.                  *theHTE      = (**theDocument).theText;
  629.              }
  630.              
  631.      return(myErr);
  632.      
  633.     } /* SetSelectionOfAppleEventDirectObject */
  634.     
  635. /**--------------------------------------------------------------------
  636.     Name             : SetSelectionOfAppleEventObject
  637.     Function     : Resolves the whatObject type of the AppleEvent into a text
  638.                              token and sets the selection to be that specified in the
  639.                              text token.
  640.                              Returns the doc and TEHandle chosen.
  641.     --------------------------------------------------------------------**/
  642.  
  643. pascal OSErr SetSelectionOfAppleEventObject(OSType            whatObject,
  644.                                                                                     const AppleEvent *theAppleEvent,
  645.                                                                                      DPtr             *theDocument,
  646.                                                                                     TEHandle         *theHTE)
  647.     {
  648.         DescType   returnedType;
  649.         long       actSize;
  650.         TextToken  myTextToken;
  651.         OSErr      paramErr;
  652.             
  653.         paramErr  = AEGetParamPtr(theAppleEvent,
  654.                                                             whatObject,
  655.                                                             typeMyText,
  656.                                                             &returnedType,
  657.                                                       (Ptr)&myTextToken,
  658.                                                             sizeof(myTextToken),
  659.                                                             &actSize);
  660.                                       
  661.         if (paramErr == noErr) 
  662.             {
  663.                 /* got a text token */
  664.                 
  665.                 *theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  666.                 *theHTE      = (**theDocument).theText;
  667.                 
  668.                 TESetSelect(myTextToken.tokenOffset-1,
  669.                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  670.                                         *theHTE);
  671.             }
  672.                              
  673.         return(paramErr);
  674.                  
  675.     } /* SetSelectionOfAppleEventObject */
  676.     
  677. /* -----------------------------------------------------------------------
  678.         Name:             AddErrorTextToReply
  679.         Purpose:        Adds the text to cannot modify subscriber
  680.      -----------------------------------------------------------------------**/
  681.      
  682. pascal void AddErrorTextToReply(const Str255 theText, AppleEvent *reply)
  683.     {
  684.         OSErr myErr;
  685.         
  686.         if (reply->descriptorType!=typeNull)
  687.             myErr = AEPutParamPtr(reply,
  688.                             keyErrorString,
  689.                             typeChar,
  690.                             (Ptr) &theText[1],
  691.                             theText[0]);
  692.     }
  693.     
  694. /* -----------------------------------------------------------------------
  695.         Name:             DoCutEdit
  696.         Purpose:        Performs a cut text operation on the current text selection
  697.      -----------------------------------------------------------------------**/
  698.     
  699. pascal OSErr DoCutEdit(const AppleEvent *theAppleEvent, AppleEvent *reply ,long refCon)
  700.     {
  701. #pragma unused (reply,refCon)
  702.     
  703.       OSErr    myErr;
  704.       TEHandle theHTE;
  705.         DPtr     theDocument;
  706.     
  707.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent,&theDocument,&theHTE);
  708.         
  709.         if (myErr==noErr) 
  710.             {            
  711.                 myErr = (OSErr) ZeroScrap();
  712.                 
  713.                 TECut(theHTE);
  714.                 
  715.                 AdjustScrollbars(theDocument, false);
  716.                 DrawPageExtras(theDocument);
  717.                 theDocument->dirty = true;
  718.             }
  719.             
  720.         return(myErr);
  721.     } /* DoCutEdit */
  722.     
  723. /* -----------------------------------------------------------------------
  724.         Name:             DoCopyEdit
  725.         Purpose:        Performs a copy text operation on the text selection specified
  726.                                 by the appleEvent direct object (if any)
  727.      -----------------------------------------------------------------------**/
  728.     
  729. pascal OSErr DoCopyEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refCon)
  730.     {
  731. #pragma unused (reply,refCon)
  732.     
  733.       OSErr    myErr;
  734.         TEHandle theHTE;
  735.         DPtr     theDocument;
  736.  
  737.         /*
  738.                 Here we extract the information about what to copy from the
  739.                 directObject - if any
  740.         */
  741.         
  742.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent,&theDocument,&theHTE);
  743.         
  744.         if (myErr==noErr) 
  745.             {
  746.                 myErr = (OSErr) ZeroScrap();
  747.                 TECopy(theHTE);     
  748.             }
  749.             
  750.         if (myErr==noErr) 
  751.             {
  752.                 myErr = SetSelectionOfAppleEventObject(keyAEContainer, 
  753.                                                                                              theAppleEvent,
  754.                                                                                              &theDocument,
  755.                                                                                              &theHTE);
  756.                 if (myErr == noErr)
  757.                 {
  758.                     TEStylPaste(theHTE);
  759.         
  760.                     AdjustScrollbars(theDocument, false);
  761.                     DrawPageExtras(theDocument);
  762.                     
  763.                     theDocument->dirty = true;
  764.                 }
  765.             }
  766.             
  767.         return(myErr);
  768.     } /* DoCopyEdit */
  769.     
  770. /* -----------------------------------------------------------------------
  771.         Name:             DoPasteEdit
  772.         Purpose:        Performs a paste text operation on the text selection specified
  773.                                 by the appleEvent direct object (if any)
  774.      -----------------------------------------------------------------------**/
  775.     
  776. pascal OSErr DoPasteEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refCon)
  777.     {
  778. #pragma unused (reply,refCon)
  779.     
  780.       OSErr    myErr;
  781.         TEHandle theHTE;
  782.         DPtr     theDocument;
  783.  
  784.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent, &theDocument, &theHTE);
  785.         
  786.         if (myErr==noErr)
  787.         {
  788.             TEStylPaste(theHTE);     
  789.             
  790.             AdjustScrollbars(theDocument, false);
  791.             DrawPageExtras(theDocument);
  792.  
  793.             theDocument->dirty = true;
  794.         }
  795.  
  796.         return(myErr);
  797.     } /* DoPasteEdit */
  798.     
  799. /* -----------------------------------------------------------------------
  800.         Name:             DoDeleteEdit
  801.         Purpose:        Performs a delete text operation on the selection specified
  802.                                 by the appleEvent direct object (if any)
  803.      -----------------------------------------------------------------------**/
  804.      
  805. pascal OSErr DoDeleteEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refcon)
  806.     {
  807. #pragma unused (reply,refcon)
  808.     
  809.         OSErr     myErr;
  810.         TEHandle theHTE;
  811.         DPtr     theDocument;
  812.             
  813.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent, &theDocument, &theHTE);
  814.         
  815.         if (myErr==noErr)
  816.         {
  817.             TEDelete(theHTE);
  818.             
  819.             theDocument->dirty = true;
  820.             AdjustScrollbars(theDocument, false);
  821.             DrawPageExtras(theDocument);
  822.         }
  823.  
  824.         return(myErr);
  825.     } /*DoDeleteEdit*/
  826.     
  827. /* -----------------------------------------------------------------------
  828.         Name:             SetTheFontOfTheTokenText
  829.         Purpose:        Sets the font of the text specified by theTextToken to 
  830.                                 the font in name.
  831.      -----------------------------------------------------------------------**/
  832.      
  833. pascal OSErr SetTheFontOfTheTokenText(TextToken theTextToken, Str255 name)
  834.     {
  835.       DPtr      theDocument;
  836.         short     theNumber;
  837.         short     theItem;
  838.         GrafPtr   oldPort;
  839.         TextStyle newStyle;
  840.         OSErr     myErr;
  841.     
  842.         myErr = noErr;
  843.         GetPort(&oldPort);
  844.         
  845.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  846.                 
  847.         GetFNum(name, &theNumber);
  848.         
  849.         theItem = ItemForNamedFont(name); /* returns 0 if failed - i.e. SystemFont */
  850.         
  851.         if (gFontMItem)
  852.             CheckItem(myMenus[fontM], gFontMItem, false);
  853.         
  854.         gFontMItem = theItem;
  855.         CheckItem(myMenus[fontM], gFontMItem, true);
  856.         
  857.         theDocument->theFont = theNumber;
  858.         
  859.         TESetSelect(theTextToken.tokenOffset-1,
  860.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  861.                                 theDocument->theText);
  862.  
  863.         newStyle.tsFont = theNumber;
  864.         TESetStyle(doFont, &newStyle, true, theDocument->theText); 
  865.         
  866.         AdjustScrollbars(theDocument, false);
  867.         DrawPageExtras(theDocument);
  868.         theDocument->dirty = true;
  869.  
  870.         SetPort(oldPort);
  871.     
  872.         return(myErr);
  873.     } /* SetTheFontOfTheTokenText */
  874.     
  875. /* -----------------------------------------------------------------------
  876.         Name:             SetTheSizeOfTheTokenText
  877.         Purpose:        Sets the size of the text specified by theTextToken to 
  878.                                 the size in theSize.
  879.      -----------------------------------------------------------------------**/
  880.      
  881. pascal OSErr SetTheSizeOfTheTokenText(TextToken theTextToken, short theSize)
  882.     {
  883.         DPtr      theDocument;
  884.         GrafPtr   oldPort;
  885.         TextStyle newStyle;
  886.         OSErr     myErr;
  887.     
  888.         myErr = noErr;
  889.         GetPort(&oldPort);
  890.         
  891.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  892.                                 
  893.         theDocument->theSize = theSize;
  894.         
  895.         TESetSelect(theTextToken.tokenOffset-1,
  896.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  897.                                 theDocument->theText);
  898.  
  899.         newStyle.tsSize = theSize;
  900.         TESetStyle(doSize, &newStyle, true, theDocument->theText); 
  901.         
  902.         AdjustScrollbars(theDocument, false);
  903.         DrawPageExtras(theDocument);
  904.         theDocument->dirty = true;
  905.  
  906.         SetPort(oldPort);
  907.         return(myErr);
  908.     } /* SetTheSizeOfTheTokenText */
  909.     
  910. /* -----------------------------------------------------------------------
  911.         Name:             SetTheStyleOfTheTokenText
  912.         Purpose:        Sets the style of the text specified by theTextToken to 
  913.                                 the style in theStyle.
  914.      -----------------------------------------------------------------------**/
  915.      
  916. pascal OSErr SetTheStyleOfTheTokenText(const TextToken *theTextToken, Style onStyle, Style offStyle)
  917.     {
  918.       DPtr      theDocument;
  919.         GrafPtr   oldPort;
  920.         TextStyle newStyle;
  921.         short     mode;
  922.         Boolean   wasContinuous;
  923.         OSErr     myErr;
  924.     
  925.         myErr = noErr;
  926.         
  927.         GetPort(&oldPort);
  928.         
  929.         theDocument = DPtrFromWindowPtr(theTextToken->tokenWindow);
  930.                                 
  931.         theDocument->theStyle = onStyle;
  932.         TESetSelect(theTextToken->tokenOffset-1,
  933.                                 theTextToken->tokenOffset+theTextToken->tokenLength-1,
  934.                                 theDocument->theText);
  935.  
  936.         // Check to see if off styles are on for whole selection
  937.         mode = doFace;
  938.         
  939.         wasContinuous = TEContinuousStyle(&mode, &newStyle, theDocument->theText);
  940.         if ((newStyle.tsFace & offStyle) != offStyle) // not off styles are on for all
  941.             {
  942.               // switch on across board so that toggle off will clear all
  943.             newStyle.tsFace  = offStyle - (newStyle.tsFace & offStyle);
  944.                 TESetStyle(doFace+doToggle, &newStyle, false, theDocument->theText); 
  945.             }
  946.             
  947.         newStyle.tsFace  = offStyle;
  948.         TESetStyle(doFace+doToggle, &newStyle,(onStyle==0), theDocument->theText); // toggle all to off
  949.         
  950.         mode = doFace;
  951.         if (onStyle)
  952.             {
  953.                 wasContinuous = TEContinuousStyle(&mode, &newStyle, theDocument->theText);
  954.                 if ((newStyle.tsFace & onStyle) != onStyle) // are they on for only a few chars
  955.                     { 
  956.                         // Need to make all chars have these characteristics
  957.                     
  958.                         newStyle.tsFace = onStyle - (newStyle.tsFace & onStyle); // take out those continuous
  959.                         TESetStyle(doFace+doToggle, &newStyle, true, theDocument->theText);
  960.                     }
  961.                 else
  962.                     TESetStyle(0, &newStyle, true, theDocument->theText); // Just Draw it, no changes
  963.             }
  964.             
  965.         AdjustScrollbars(theDocument, false);
  966.         DrawPageExtras(theDocument);
  967.         theDocument->dirty = true;
  968.         
  969.         SetPort(oldPort);
  970.         
  971.         return(myErr);
  972.     } /* SetTheStyleOfTheTokenText */
  973.  
  974. /* -----------------------------------------------------------------------
  975.         Name:             SetWindowProperty
  976.         Purpose:        Sets the window property specified in theWindowPropToken to
  977.                                 be that supplied in dataDesc.
  978.      -----------------------------------------------------------------------**/
  979.      
  980. pascal OSErr SetWindowProperty(const AEDesc *theWPTokenDesc, const AEDesc *dataDesc)
  981.     {
  982.       Str255          theNewName;
  983.         DPtr            theDocument;
  984.         OSErr           err;
  985.         OSErr           ignoreErr;
  986.         Rect            thePosnRect;
  987.         Boolean         theBoolean;
  988.         TEHandle        theHTE;
  989.         GrafPtr         oldPort;
  990.         Point           thePosn;
  991.         THPrint         theTHPrint;
  992.         windowPropToken theWindowPropToken;
  993.         AEDesc          newDesc;
  994.         AEDesc          tokenDesc;
  995.         Size            tokenSize;
  996.         TextToken       myTextToken;
  997.         short           hOffset;
  998.         short           vOffset;
  999.  
  1000.         err = AECoerceDesc(theWPTokenDesc, typeMyWindowProp, &newDesc);
  1001.         
  1002.         if (err)
  1003.             return(err);
  1004.  
  1005.         GetRawDataFromDescriptor(&newDesc,
  1006.                                                          (Ptr)&theWindowPropToken,
  1007.                                                          sizeof(theWindowPropToken),
  1008.                                                          &tokenSize);
  1009.         
  1010.         ignoreErr = AEDisposeDesc(&newDesc);
  1011.     
  1012.         GetPort(&oldPort);
  1013.         SetPort(theWindowPropToken.tokenWindowToken);
  1014.         
  1015.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1016.         
  1017.         switch (theWindowPropToken.tokenProperty) {
  1018.         
  1019.             case pName:
  1020.                             err = GetPStringFromDescriptor(dataDesc, (char *)theNewName);
  1021.                             if (err==noErr) 
  1022.                                 if (theNewName[0] == 0) 
  1023.                                     err = errAEWrongDataType;
  1024.                                 else
  1025.                                     {
  1026.                                         SetWTitle(theWindowPropToken.tokenWindowToken, theNewName);
  1027.                                         PLstrcpy(theDocument->theFileName, theNewName); /* Should we do this??? */
  1028.                                         theDocument->dirty = true;
  1029.                                     }
  1030.                             break;
  1031.             
  1032.             case pContents:
  1033.                             theHTE = theDocument->theText;
  1034.                             TESetSelect(0, 32000, theHTE);
  1035.                             TEDelete(theHTE);
  1036.                             err = GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1037.                             theDocument->dirty = true;
  1038.                             break;
  1039.                             
  1040.             case pBounds:
  1041.                             err = GetRectFromDescriptor(dataDesc, &thePosnRect);
  1042.                             /* the rectangle is for the structure region, and is in global coordinates */
  1043.                             /* MoveWindow and SizeWindow apply to the content region, so we have to massage a little */
  1044.             
  1045.                             thePosnRect.top    += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.top -
  1046.                                                                         (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.top;
  1047.                                                                         
  1048.                             thePosnRect.left   += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.left -
  1049.                                                                         (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.left;
  1050.                                                                         
  1051.                             thePosnRect.bottom += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.bottom -
  1052.                                                                         (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.bottom;
  1053.                                                                         
  1054.                             thePosnRect.right  += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.right -
  1055.                                                                         (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.right;
  1056.                             
  1057.                             if (EmptyRect(&thePosnRect)) 
  1058.                                 err = errAECorruptData;
  1059.                             else
  1060.                                 {
  1061.                                     MoveWindow(theWindowPropToken.tokenWindowToken,
  1062.                                                          thePosnRect.left,
  1063.                                                          thePosnRect.top,
  1064.                                                          false);
  1065.                                     SizeWindow(theWindowPropToken.tokenWindowToken,
  1066.                                                          thePosnRect.right- thePosnRect.left,
  1067.                                                          thePosnRect.bottom-thePosnRect.top,
  1068.                                                          true);
  1069.                                     ResizeWindow(theDocument);
  1070.                                 }
  1071.                             break;
  1072.  
  1073.             case pPosition:
  1074.                             err = GetPointFromDescriptor(dataDesc, &thePosn);
  1075.                             /* the point is for the structure region, and is in global coordinates */
  1076.                             /* MoveWindow applies to the content region, so we have to massage a little */
  1077.                             
  1078.                             hOffset = (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.left -
  1079.                                                 (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.left;
  1080.                                                 
  1081.                             vOffset = (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.top -
  1082.                                                 (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.top;
  1083.                                                 
  1084.                                                 
  1085.                             thePosn.v  += vOffset;
  1086.                             thePosn.h  += hOffset;
  1087.                             
  1088.                             MoveWindow(theWindowPropToken.tokenWindowToken,
  1089.                                                  thePosn.h,
  1090.                                                  thePosn.v,
  1091.                                                  false);
  1092.                                                  
  1093.                             ResizeWindow(theDocument);
  1094.                             break;
  1095.             
  1096.           case pIsZoomed:
  1097.                             err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1098.                             if (theBoolean)
  1099.                                 ZoomWindow(qd.thePort,
  1100.                                                      inZoomOut,
  1101.                                                      false);
  1102.                             else
  1103.                                 ZoomWindow(qd.thePort,
  1104.                                                      inZoomIn,
  1105.                                                      false);
  1106.                                                                     
  1107.                             ResizeWindow(theDocument);
  1108.                             break;
  1109.             
  1110.             case pVisible:
  1111.                             err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1112.                             if (theBoolean)
  1113.                                 ShowWindow(theWindowPropToken.tokenWindowToken);
  1114.                             else
  1115.                                 HideWindow(theWindowPropToken.tokenWindowToken);
  1116.                             break;
  1117.             
  1118.             case pPageSetup:
  1119.                             err = GetTHPrintFromDescriptor(dataDesc, &theTHPrint);
  1120.                                 
  1121.                             if (theTHPrint) 
  1122.                                 {
  1123.                                     if (theDocument->thePrintSetup) 
  1124.                                         DisposHandle((Handle)theDocument->thePrintSetup);
  1125.                                         
  1126.                                     theDocument->thePrintSetup = theTHPrint;
  1127.                                     theDocument->dirty = true;
  1128.                                     ResizePageSetupForDocument(theDocument);
  1129.                                 }
  1130.                             break;
  1131.  
  1132.             case pSelection:
  1133.                             err = AECoerceDesc(dataDesc, typeMyText, &tokenDesc);
  1134.                             
  1135.                             GetRawDataFromDescriptor(&tokenDesc,
  1136.                                                                              (Ptr)&myTextToken,
  1137.                                                                              sizeof(myTextToken),
  1138.                                                                              &tokenSize);
  1139.                                                                                              
  1140.                             ignoreErr = AEDisposeDesc(&tokenDesc);
  1141.                             
  1142.                             if (err == noErr) 
  1143.                                 {
  1144.                                     /* got a text token */
  1145.                                     
  1146.                                     theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  1147.                                     theHTE      = theDocument->theText;
  1148.                                     
  1149.                                     TESetSelect(myTextToken.tokenOffset-1,
  1150.                                                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  1151.                                                             theHTE);
  1152.                                 }
  1153.                             break;
  1154.             
  1155.             case pIndex:
  1156.             case pIsModal:
  1157.             case pIsResizable:
  1158.             case pHasTitleBar:
  1159.             case pHasCloseBox:
  1160.             case pIsFloating:
  1161.             case pBestType:
  1162.             case pClass:
  1163.             case pDefaultType:
  1164.             case pIsZoomable:
  1165.             case pIsModified:
  1166.             default:
  1167.                             err = errAEEventNotHandled; /* We don't allow these to be set or we don't understand it*/
  1168.         }
  1169.         
  1170.         SetPort(oldPort);
  1171.         
  1172.         return(err);
  1173.                 
  1174.     } /* SetWindowProperty */
  1175.     
  1176. /* -----------------------------------------------------------------------
  1177.         Name:             AddDescStyleItem
  1178.         Purpose:        Adds the kAEXXXX style to theStyle.
  1179.      -----------------------------------------------------------------------**/
  1180.      
  1181. pascal void AddDescStyleItem(DescType theDesc, Style *theStyle)
  1182.     {
  1183.         if (theDesc == kAEBold)
  1184.             *theStyle = *theStyle+bold;
  1185.         else
  1186.         if (theDesc == kAEItalic)
  1187.             *theStyle = *theStyle+italic;
  1188.         else
  1189.         if (theDesc == kAEUnderline)
  1190.             *theStyle = *theStyle+underline;
  1191.         else
  1192.         if (theDesc == kAEOutline)
  1193.             *theStyle = *theStyle+outline;
  1194.         else
  1195.         if (theDesc == kAEShadow)
  1196.             *theStyle = *theStyle+shadow;
  1197.         else
  1198.         if (theDesc == kAECondensed)
  1199.             *theStyle = *theStyle+condense;
  1200.         else
  1201.         if (theDesc == kAEExpanded)
  1202.             *theStyle = *theStyle+extend;
  1203.         else
  1204.         if (theDesc == kAEPlain)
  1205.             *theStyle = 0;
  1206.     } /* AddDescStyleItem */
  1207.     
  1208. pascal OSErr MakeStyleFromAEList(const AEDescList *styleList, Style *theStyle, Boolean *hadPlain)
  1209.     {
  1210.         OSErr     myErr;
  1211.         DescType  styleDesc;
  1212.         long      itemsInList;
  1213.         long      actSize;
  1214.         AEKeyword keywd;
  1215.         DescType  typeCode;
  1216.             
  1217.         *hadPlain = false;
  1218.         *theStyle = 0;
  1219.         
  1220.       myErr = AECountItems(styleList, &itemsInList);
  1221.         while (itemsInList>0)
  1222.           if (myErr==noErr)
  1223.                 {
  1224.                     myErr  = AEGetNthPtr(styleList,
  1225.                                                              itemsInList,
  1226.                                                              typeEnumerated,
  1227.                                                              &keywd,
  1228.                                                              &typeCode,
  1229.                                                              (Ptr)&styleDesc,
  1230.                                                              sizeof(styleDesc),
  1231.                                                              &actSize);
  1232.                     
  1233.                     AddDescStyleItem(styleDesc, theStyle);
  1234.                     
  1235.                     if (styleDesc == kAEPlain) 
  1236.                       {
  1237.                             itemsInList = 0;
  1238.                             *hadPlain    = true;
  1239.                         }
  1240.                     else
  1241.                       itemsInList--;
  1242.                 }
  1243.                 
  1244.         return(myErr);
  1245.     } /*MakeStyleFromAEList*/
  1246.     
  1247. pascal OSErr GetTextStyles(const AEDesc *dataDesc, Style *onStyles, Style *offStyles)
  1248.     {
  1249.         OSErr      myErr;
  1250.         OSErr      ignoreErr;
  1251.         AEDescList textSDesc;
  1252.         AEDescList onDesc;
  1253.         AEDescList offDesc;
  1254.         Boolean    hadPlain;
  1255.             
  1256.       textSDesc.dataHandle = nil;
  1257.       onDesc.dataHandle    = nil;
  1258.       offDesc.dataHandle   = nil;
  1259.         
  1260.         *onStyles  = 0;
  1261.         *offStyles = 0;
  1262.         
  1263.       myErr = AECoerceDesc(dataDesc, typeAERecord, &textSDesc);
  1264.         
  1265.         if (myErr==noErr)
  1266.           myErr = AEGetKeyDesc(&textSDesc, keyAEOnStyles, typeAEList, &onDesc);
  1267.         
  1268.         if (myErr==noErr)
  1269.           myErr = AEGetKeyDesc(&textSDesc, keyAEOffStyles, typeAEList, &offDesc);
  1270.         
  1271.         if (myErr==noErr)
  1272.             myErr = MakeStyleFromAEList(&onDesc,  onStyles, &hadPlain);
  1273.             
  1274.         if (hadPlain)
  1275.             *offStyles = bold+italic+underline+outline+shadow+condense+extend;
  1276.         else
  1277.             {
  1278.                 if (myErr==noErr)
  1279.                     myErr = MakeStyleFromAEList(&offDesc, offStyles, &hadPlain);
  1280.         
  1281.                 if (hadPlain)
  1282.                     myErr = errAEEventFailed;
  1283.             }
  1284.             
  1285.         if (textSDesc.dataHandle)
  1286.             ignoreErr = AEDisposeDesc(&textSDesc);
  1287.             
  1288.         if (onDesc.dataHandle)
  1289.             ignoreErr = AEDisposeDesc(&onDesc);
  1290.             
  1291.         if (offDesc.dataHandle)
  1292.             ignoreErr = AEDisposeDesc(&offDesc);
  1293.         
  1294.         return(myErr);
  1295.     } /* GetTextStyles */
  1296.  
  1297. /* -----------------------------------------------------------------------
  1298.         Name:             SetTextProperty
  1299.         Purpose:        Sets the text property specfied by theTextPropToken to
  1300.                                 that in dataDesc.
  1301.      -----------------------------------------------------------------------**/
  1302.      
  1303. pascal OSErr SetTextProperty(const AEDesc *tokenDesc, const AEDesc *dataDesc)
  1304.     {
  1305.       TEHandle      theHTE;
  1306.         DPtr          theDoc;
  1307.         Str255        name;
  1308.         short         theSize;
  1309.         OSErr         myErr;
  1310.         OSErr         ignoreErr;
  1311.         Style         onStyle;
  1312.         Style         offStyle;
  1313.         textPropToken theTextPropToken;
  1314.         AEDesc        newDesc;
  1315.         Size          tokenSize;
  1316.         
  1317.       newDesc.dataHandle = nil;
  1318.         
  1319.       myErr = AECoerceDesc(tokenDesc, typeMyTextProp, &newDesc);
  1320.         if (myErr == noErr)
  1321.             {
  1322.                 GetRawDataFromDescriptor(&newDesc,
  1323.                                                                  (Ptr)&theTextPropToken,
  1324.                                                                  sizeof(theTextPropToken),
  1325.                                                                  &tokenSize);
  1326.                 ignoreErr = AEDisposeDesc(&newDesc);
  1327.             }
  1328.         else
  1329.             return(myErr);
  1330.             
  1331.         theDoc = DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1332.         theDoc->dirty = true;
  1333.         
  1334.         switch (theTextPropToken.propertyProperty) {
  1335.         
  1336.             case pContents:
  1337.                         theHTE = theDoc->theText;
  1338.                                     TESetSelect(theTextPropToken.propertyTextToken.tokenOffset-1,
  1339.                                                             theTextPropToken.propertyTextToken.tokenOffset+
  1340.                                                                                     theTextPropToken.propertyTextToken.tokenLength-1,
  1341.                                                             theHTE);
  1342.  
  1343.                                     TEDelete(theHTE);
  1344.                                     myErr = GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1345.                                     break;
  1346.             
  1347.             case pFont: myErr = GetPStringFromDescriptor(dataDesc, (char *)name);
  1348.                                     
  1349.                                     if (myErr==noErr)
  1350.                                         myErr = SetTheFontOfTheTokenText(theTextPropToken.propertyTextToken,
  1351.                                                                                                          name);
  1352.                                     break;
  1353.             
  1354.           case pPointSize:
  1355.                         myErr = GetIntegerFromDescriptor(dataDesc, &theSize);
  1356.                                     if (myErr==noErr)
  1357.                                         myErr = SetTheSizeOfTheTokenText(theTextPropToken.propertyTextToken,
  1358.                                                                                                           theSize);
  1359.                                     break;
  1360.                                     
  1361.             case pTextStyles:
  1362.                                     onStyle  = 0;
  1363.                                     offStyle = 0;
  1364.                                     
  1365.                                     myErr = GetTextStyles(dataDesc, &onStyle, &offStyle);
  1366.                     
  1367.                                     if (myErr==noErr)
  1368.                                         if (onStyle & offStyle != 0)
  1369.                                             myErr = errAEEventFailed;
  1370.                                         else
  1371.                                             myErr = SetTheStyleOfTheTokenText(&theTextPropToken.propertyTextToken,
  1372.                                                                                                                 onStyle,
  1373.                                                                                                                 offStyle);
  1374.                                     break;
  1375.                                     
  1376.             default: myErr = errAEWrongDataType;
  1377.             
  1378.         }
  1379.                         
  1380.         return(myErr);
  1381.             
  1382.     } /* SetTextProperty */
  1383.     
  1384. /* -----------------------------------------------------------------------
  1385.         Name:             HandleSetData
  1386.         Purpose:        Resolves the object into a token (could be one of many) and
  1387.                                 the sets the data of that object to dataDesc.
  1388.      -----------------------------------------------------------------------**/
  1389.      
  1390. pascal OSErr HandleSetData(const AEDesc *theObj, const AEDesc *dataDesc, AppleEvent *reply)
  1391. {
  1392.   OSErr           myErr;
  1393.   OSErr           ignoreErr;
  1394.     AEDesc          newDesc;
  1395.     DPtr            theDocument;
  1396.     TEHandle        theHTE;
  1397.     TextToken       theTextToken;
  1398.     Size            tokenSize;
  1399.     AEDesc          objTokenDesc;
  1400.     
  1401.     objTokenDesc.dataHandle = nil;
  1402.     newDesc.dataHandle      = nil;
  1403.     
  1404.     /*
  1405.         Coerce theObj into a token which we can use - 
  1406.              set the property or data for that token
  1407.     */
  1408.     
  1409.     myErr = AEResolve(theObj ,kAEIDoMinimum, &objTokenDesc);
  1410.         
  1411.     /* We don't actually allow ANY app property setting, but
  1412.        just incase we'll decode looking for an typeMyApplProp and flag an error -
  1413.          do same for menu related tokens
  1414.     */
  1415.     
  1416.     switch (objTokenDesc.descriptorType) {
  1417.         case typeMyWindowProp: myErr = SetWindowProperty(&objTokenDesc, dataDesc);
  1418.                                                      break;
  1419.         
  1420.         case typeMyTextProp :  myErr = SetTextProperty(&objTokenDesc, dataDesc);
  1421.                                break;
  1422.  
  1423.         case typeMyText:        if (AECoerceDesc(&objTokenDesc, typeMyText, &newDesc) == noErr) 
  1424.                                                         {
  1425.                                                             GetRawDataFromDescriptor(&newDesc,
  1426.                                                                                                              (Ptr)&theTextToken,
  1427.                                                                                                              sizeof(theTextToken),
  1428.                                                                                                              &tokenSize);
  1429.                                                                                                              
  1430.                                                             ignoreErr = AEDisposeDesc(&newDesc);
  1431.                                                                                                              
  1432.                                                             theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  1433.                                                             theHTE            = theDocument->theText;
  1434.                                                             
  1435.                                                             TESetSelect(theTextToken.tokenOffset-1,
  1436.                                                                                     theTextToken.tokenOffset+
  1437.                                                                                                             theTextToken.tokenLength-1,
  1438.                                                                                     theHTE);
  1439.  
  1440.                                                             TEDelete(theHTE);
  1441.                                                                 
  1442.                                                             myErr = GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1443.                                                                 
  1444.                                                             theDocument->dirty = true;
  1445.                                                         }
  1446.                                                     break;
  1447.                                                     
  1448.         // Read only items and properties and those we don't know
  1449.         case typeMyApplProp:
  1450.         case typeMyMenu:
  1451.         case typeMyMenuProp:
  1452.         case typeMyMenuItem:
  1453.         case typeMyItemProp:                                                        
  1454.         default:            myErr = errAEWrongDataType;
  1455.     }
  1456.     
  1457.     if (objTokenDesc.dataHandle)
  1458.         ignoreErr = AEDisposeDesc(&objTokenDesc);
  1459.     
  1460.     if (myErr == errCantWriteSubscriber)
  1461.         AddErrorTextToReply((unsigned char *)"\pCannot write into subscriber", reply);
  1462.         
  1463.     return(myErr);
  1464. }    /* HandleSetData */
  1465.  
  1466. /*
  1467.     A few convenient FORWARDS...
  1468. */
  1469.  
  1470. pascal OSErr MakeWindowObj( WindowPtr theWindow, AEDesc *dMyDoc);
  1471. pascal OSErr MakeTextObj(WindowPtr theWindow,
  1472.                                                  short     selStart,
  1473.                                                  short     selEnd,
  1474.                                                  AEDesc    *selTextObj);
  1475.  
  1476. pascal OSErr MakeMenuObj(Str255 theName, AEDesc *resultingObj);
  1477. pascal OSErr MakeMenuItemObj(Str255 theName, short theItem, AEDesc *resultingObj);
  1478. pascal OSErr MakeClassObj(DescType theClass, AEDesc *myClassObj);
  1479. /*
  1480.     Back to real code
  1481. */
  1482. pascal OSErr MakeSelTextObj( WindowPtr theWindow,
  1483.                                                          TEHandle  theTextEditHandle,
  1484.                                                          AEDesc    *selTextObj)
  1485. /*
  1486.     This is a hack to get the AppleScript Alpha to work...
  1487. */
  1488.     {
  1489.         OSErr    myErr;
  1490.         OSErr    ignoreErr;
  1491.         AEDesc   dNull;
  1492.         AEDesc   dMyDoc;
  1493.         AEDesc   startOfs;
  1494.         AEDesc   endOfs;
  1495.         AEDesc   startObj;
  1496.         AEDesc   endObj;
  1497.         AEDesc   rangeDesc;
  1498.         long     startChar;
  1499.         long     endChar;
  1500.         Boolean  spotFlag;
  1501.             
  1502.         myErr = noErr;
  1503.         
  1504.         if (theWindow==nil) 
  1505.             return(noErr);
  1506.             
  1507.         selTextObj->dataHandle = nil;
  1508.         dMyDoc.dataHandle      = nil;
  1509.         startObj.dataHandle    = nil;
  1510.         endObj.dataHandle      = nil;
  1511.         
  1512.         /* 
  1513.             make the window object 
  1514.         */
  1515.         
  1516.         myErr = MakeWindowObj(theWindow, &dMyDoc);
  1517.             
  1518.         if (myErr==noErr)
  1519.             {
  1520.                 /* get the start and end of selection */
  1521.                 
  1522.                 startChar = (*theTextEditHandle)->selStart+1;    /* start counting obj's from 1, not 0 */
  1523.                 endChar   = (*theTextEditHandle)->selEnd;
  1524.                 spotFlag  = ((*theTextEditHandle)->selStart == (*theTextEditHandle)->selEnd);
  1525.                     
  1526.                 myErr = CreateOffsetDescriptor(startChar, &startOfs);
  1527.                             
  1528.                 if (myErr==noErr)
  1529.                     if (spotFlag)
  1530.                         myErr = CreateObjSpecifier(cSpot,
  1531.                                                                              &dMyDoc,
  1532.                                                                              formAbsolutePosition, 
  1533.                                                                              &startOfs, 
  1534.                                                                              true, 
  1535.                                                                              selTextObj);
  1536.                     else
  1537.                         {
  1538.                             /* not a spot - must represent as range */
  1539.                             /* make obj for start char */
  1540.                             
  1541.                             myErr = AECreateDesc(typeNull, nil , 0, &dNull);
  1542.                             
  1543.                             myErr = CreateObjSpecifier(cChar,
  1544.                                                        &dNull,
  1545.                                                                                  formAbsolutePosition,
  1546.                                                                                  &startOfs,
  1547.                                                                                  false,
  1548.                                                                                  &startObj);
  1549.                             
  1550.                             if (myErr==noErr) 
  1551.                                 myErr = CreateOffsetDescriptor(endChar, &endOfs);
  1552.                             
  1553.                             if (myErr==noErr) 
  1554.                                 myErr = CreateObjSpecifier(cChar,
  1555.                                                                                      &dNull,
  1556.                                                                                      formAbsolutePosition,
  1557.                                                                                      &endOfs,
  1558.                                                                                      false,
  1559.                                                                                      &endObj);
  1560.                             
  1561.                             if (myErr==noErr) 
  1562.                                 myErr = CreateRangeDescriptor(&startObj,
  1563.                                                                                             &endObj,
  1564.                                                                                             false,
  1565.                                                                                             &rangeDesc);
  1566.                             
  1567.                             if (myErr==noErr) 
  1568.                                 myErr = CreateObjSpecifier(cChar,
  1569.                                                                                      &dMyDoc,
  1570.                                                                                      formRange,
  1571.                                                                                      &rangeDesc,
  1572.                                                                                      true,
  1573.                                                                                      selTextObj);
  1574.                                                                                      
  1575.                             if (startObj.dataHandle)
  1576.                               ignoreErr = AEDisposeDesc(&startObj);
  1577.                                 
  1578.                             if (startOfs.dataHandle)
  1579.                               ignoreErr = AEDisposeDesc(&startOfs);
  1580.                                 
  1581.                             if (endObj.dataHandle)
  1582.                               ignoreErr = AEDisposeDesc(&endObj);
  1583.                                 
  1584.                             if (endOfs.dataHandle)
  1585.                               ignoreErr = AEDisposeDesc(&endOfs);
  1586.                         }
  1587.             }
  1588.             
  1589.         return(myErr);
  1590.     }    /* MakeSelTextObj */
  1591.  
  1592. /* -----------------------------------------------------------------------
  1593.         Name:             DoSetData
  1594.         Purpose:        Handles the SetData Apple Event, extracting the direct
  1595.                                 object (which says what to set) and the data (what to set
  1596.                                 it to).
  1597.      -----------------------------------------------------------------------**/
  1598.      
  1599. pascal OSErr DoSetData(const AppleEvent *theAppleEvent,
  1600.                                                AppleEvent *reply,
  1601.                                                          long        handlerRefCon)
  1602.   {
  1603. #pragma unused (reply, handlerRefCon)
  1604.     
  1605.         OSErr  myErr;
  1606.         OSErr  ignoreErr;
  1607.         AEDesc myDirObj;
  1608.         AEDesc myDataDesc;
  1609.         
  1610.         myDataDesc.dataHandle = nil;
  1611.         myDirObj.dataHandle   = nil;
  1612.                 
  1613.         /* pick up the direct object, which is the object whose data is to be set */
  1614.         
  1615.         myErr = AEGetParamDesc(theAppleEvent,
  1616.                                                      keyDirectObject,
  1617.                                                      typeWildCard,
  1618.                                                      &myDirObj);
  1619.             
  1620.         /* now the data to set it to - typeWildCard means get as is*/
  1621.         if (myErr == noErr) 
  1622.             myErr = AEGetParamDesc( theAppleEvent,
  1623.                                                             keyAEData,
  1624.                                                             typeWildCard,
  1625.                                                             &myDataDesc);
  1626.         
  1627.         /* missing any parameters? */
  1628.         if (myErr == noErr) 
  1629.             myErr = GotRequiredParams(theAppleEvent);
  1630.         
  1631.         /* set the data */
  1632.         if (myErr == noErr) 
  1633.             myErr = HandleSetData(&myDirObj, &myDataDesc, reply);
  1634.             
  1635.         if (myDataDesc.dataHandle)
  1636.             ignoreErr = AEDisposeDesc(&myDataDesc);
  1637.             
  1638.         if (myDirObj.dataHandle)
  1639.             ignoreErr = AEDisposeDesc(&myDirObj);
  1640.   
  1641.         return(myErr);
  1642.     }    /* DoSetData */
  1643.     
  1644. pascal void StyleTokConst(short theStyleItem, DescType *thekConst)
  1645.     {
  1646.         switch (theStyleItem) {
  1647.           case bold      : *thekConst = kAEBold;
  1648.                                              break;
  1649.       case italic    : *thekConst = kAEItalic;
  1650.                                              break;
  1651.       case underline : *thekConst = kAEUnderline;
  1652.                                              break;
  1653.       case outline   : *thekConst = kAEOutline;
  1654.                                              break;
  1655.             case shadow    : *thekConst = kAEShadow;
  1656.                                              break;
  1657.             case condense  : *thekConst = kAECondensed;
  1658.                                              break;
  1659.             case extend    : *thekConst = kAEExpanded;
  1660.                                              break;
  1661.         }
  1662.     } /*StyleTokConst*/
  1663.     
  1664. pascal OSErr BuildTypeTextStylesDesc(Style onStyles, Style offStyles, AEDesc *resultDesc)
  1665.     {
  1666.  
  1667.         OSErr     myErr;
  1668.         OSErr     ignoreErr;
  1669.         short     myStyleItem;
  1670.         DescType  styleConst;
  1671.         AEDesc    onStylesDesc;
  1672.         AEDesc    offStylesDesc;
  1673.         AEDesc    dataDesc;
  1674.         
  1675.         onStylesDesc.dataHandle  = nil;
  1676.         offStylesDesc.dataHandle = nil;
  1677.         dataDesc.dataHandle  = nil;
  1678.     
  1679.         myErr = AECreateList(nil, 0, true,  &dataDesc);
  1680.         
  1681.         myErr = AECreateList(nil, 0, false, &onStylesDesc);
  1682.         myErr = AECreateList(nil, 0, false, &offStylesDesc);
  1683.         
  1684.         for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1685.             if (myErr==noErr)
  1686.                 {
  1687.                     StyleTokConst(myStyleItem, &styleConst);
  1688.                     if (myStyleItem & onStyles)
  1689.                         myErr  = AEPutPtr(&onStylesDesc,
  1690.                                                             0,                /*add to end of list*/
  1691.                                                             typeEnumerated, /* text for style name */
  1692.                                                             (Ptr)&styleConst,
  1693.                                                             sizeof(styleConst));
  1694.                     
  1695.                     if (myStyleItem & offStyles)
  1696.                         myErr  = AEPutPtr(&offStylesDesc,
  1697.                                                             0,                /*add to end of list*/
  1698.                                                             typeEnumerated, /* text for style name */
  1699.                                                             (Ptr)&styleConst,
  1700.                                                             sizeof(styleConst));
  1701.             }
  1702.         
  1703.         if (myErr==noErr)
  1704.             myErr = AEPutKeyDesc(&dataDesc, keyAEOnStyles,  &onStylesDesc);
  1705.         
  1706.         if (myErr==noErr)
  1707.             myErr = AEPutKeyDesc(&dataDesc, keyAEOffStyles, &offStylesDesc);
  1708.         
  1709.         if (myErr==noErr)
  1710.             myErr = AECoerceDesc(&dataDesc, typeTextStyles, resultDesc);
  1711.             
  1712.         if (onStylesDesc.dataHandle)
  1713.             ignoreErr = AEDisposeDesc(&onStylesDesc);
  1714.             
  1715.         if (offStylesDesc.dataHandle)
  1716.             ignoreErr = AEDisposeDesc(&offStylesDesc);
  1717.             
  1718.         if (dataDesc.dataHandle)
  1719.             ignoreErr = AEDisposeDesc(&dataDesc);
  1720.     
  1721.         return(myErr);
  1722.     }
  1723.  
  1724. pascal OSErr BuildTextStylesDesc(Style theStyle, AEDesc *resultDesc)
  1725.     {
  1726.         short     myStyleItem;
  1727.         Style     onStyles;
  1728.         Style     offStyles;
  1729.                 
  1730.         onStyles  = 0;
  1731.         offStyles = 0;
  1732.         
  1733.         for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1734.             {
  1735.                 if (myStyleItem & theStyle)
  1736.                     onStyles  = onStyles  + myStyleItem;
  1737.                 else
  1738.                     offStyles = offStyles + myStyleItem;
  1739.             }
  1740.     
  1741.         return(BuildTypeTextStylesDesc(onStyles, offStyles, resultDesc));
  1742.         
  1743.     } /* BuildTextStylesDesc */
  1744.         
  1745. pascal OSErr BuildStyledTextDesc(TEHandle theHTE, short start, short howLong, AEDesc *resultDesc)
  1746.     {
  1747.         AEDesc       listDesc;
  1748.         short        oldSelStart;
  1749.         short        oldSelEnd;
  1750.         StScrpHandle myStScrpHandle;
  1751.         OSErr        myErr;
  1752.         OSErr        ignoreErr;
  1753.         
  1754.         listDesc.dataHandle = nil;
  1755.         
  1756.         oldSelStart = (**theHTE).selStart;
  1757.         oldSelEnd   = (**theHTE).selEnd;
  1758.         
  1759.         TESetSelect(start-1, start+howLong-2, theHTE);
  1760.         
  1761.         myErr = AECreateList(nil, 0, true,  &listDesc);
  1762.         
  1763.         HLock((Handle)(**theHTE).hText);
  1764.                                                          
  1765.         if (myErr==noErr)
  1766.             myErr = AEPutKeyPtr(&listDesc,
  1767.                                 keyAEText,
  1768.                                                     typeChar,
  1769.                                                     (Ptr)&(*(**theHTE).hText)[start-1],
  1770.                                                     howLong);
  1771.                                                     
  1772.         HUnlock((Handle)(**theHTE).hText);
  1773.         
  1774.         myStScrpHandle = GetStylScrap(theHTE);
  1775.         
  1776.         if (myStScrpHandle)
  1777.             {
  1778.                 HLock((Handle)myStScrpHandle);
  1779.                 
  1780.                 if (myErr==noErr)
  1781.                     myErr = AEPutKeyPtr(&listDesc,
  1782.                                         keyAEStyles,
  1783.                                                             typeScrapStyles,
  1784.                                                             (Ptr)*myStScrpHandle,
  1785.                                                             GetHandleSize((Handle)myStScrpHandle));
  1786.                     
  1787.                 HUnlock((Handle)myStScrpHandle);
  1788.             }    
  1789.         else
  1790.             myErr = AEPutKeyPtr(&listDesc,
  1791.                                                     keyAEStyles,
  1792.                                                     typeScrapStyles,
  1793.                                                     (Ptr)nil,
  1794.                                                     0);
  1795.         
  1796.         if (myErr==noErr)
  1797.             myErr = AECoerceDesc(&listDesc, typeStyledText, resultDesc); // should be typeIntlText
  1798.         
  1799.         if (listDesc.dataHandle)
  1800.             ignoreErr = AEDisposeDesc(&listDesc);
  1801.         
  1802.         TESetSelect(oldSelStart, oldSelEnd, theHTE);
  1803.         
  1804.         return(myErr);
  1805.     }
  1806.     
  1807. /* -----------------------------------------------------------------------
  1808.         Name:             GetTextProperty
  1809.         Purpose:        Fills dataDesc with the requested text property.
  1810.      -----------------------------------------------------------------------**/
  1811.      
  1812. pascal OSErr GetTextProperty(const AEDesc *theTokenDesc, AEDesc *dataDesc)
  1813.     {
  1814.       DPtr          theDocument;
  1815.         TEHandle      theHTE;
  1816.         Str255        fontName;
  1817.         short         theSize;
  1818.         GrafPtr       oldPort;
  1819.         TextStyle     theTextStyle;
  1820.         short         lineHeight;
  1821.         short         fontAscent;
  1822.         textPropToken theTextPropToken;
  1823.         OSErr         myErr;
  1824.         OSErr                    ignoreErr;
  1825.         Size          tokenSize;
  1826.         AEDesc        newDesc;
  1827.         
  1828.       myErr = AECoerceDesc(theTokenDesc, typeMyTextProp, &newDesc);
  1829.         if (myErr == noErr)
  1830.             {
  1831.                 GetRawDataFromDescriptor(&newDesc,
  1832.                                                                  (Ptr)&theTextPropToken,
  1833.                                                                  sizeof(theTextPropToken),
  1834.                                                                  &tokenSize);
  1835.                 ignoreErr= AEDisposeDesc(&newDesc);                                                 
  1836.             }
  1837.         else
  1838.           return(myErr);
  1839.  
  1840.         /*
  1841.             For each property we build a descriptor to be returned as the reply.
  1842.         */
  1843.                 
  1844.         theDocument = DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1845.         theHTE             = theDocument->theText;
  1846.         
  1847.         TEGetStyle (theTextPropToken.propertyTextToken.tokenOffset-1,
  1848.                                 &theTextStyle,
  1849.                                 &lineHeight,
  1850.                                 &fontAscent,
  1851.                                 theHTE);
  1852.                                         
  1853.         switch (theTextPropToken.propertyProperty){ 
  1854.  
  1855.             case pContents:
  1856.             
  1857.                     myErr = BuildStyledTextDesc(theHTE,
  1858.                                                                             theTextPropToken.propertyTextToken.tokenOffset,
  1859.                                                                             theTextPropToken.propertyTextToken.tokenLength,
  1860.                                                                             dataDesc);
  1861.                     break;
  1862.             
  1863.             case pFont:
  1864.             
  1865.                     GetFontName(theTextStyle.tsFont, fontName);
  1866.                     
  1867.                     myErr = AECreateDesc(typeChar,
  1868.                                                              (Ptr)&fontName[1],
  1869.                                                              fontName[0],
  1870.                                                              dataDesc);
  1871.                     break;
  1872.                     
  1873.             case pTextStyles:
  1874.             
  1875.                     myErr = BuildTextStylesDesc(theTextStyle.tsFace, dataDesc);
  1876.                     break;
  1877.  
  1878.             case pPointSize:
  1879.  
  1880.                     myErr =CreateOffsetDescriptor(theTextStyle.tsSize, dataDesc);
  1881.                     break;
  1882.                     
  1883.             case pScriptTag:
  1884.                     
  1885.                     myErr = CreateOffsetDescriptor(smSystemScript, dataDesc);
  1886.                     break;
  1887.             
  1888.             case pStringWidth:
  1889.                 
  1890.                     GetPort(&oldPort);
  1891.                     SetPort(theTextPropToken.propertyTextToken.tokenWindow);
  1892.                     
  1893.                     HLock((Handle)(**theHTE).hText);
  1894.                     theSize = TextWidth(&(**theHTE).hText,
  1895.                                                             theTextPropToken.propertyTextToken.tokenOffset-1,
  1896.                                                             theTextPropToken.propertyTextToken.tokenLength);
  1897.                     
  1898.                     HUnlock((Handle)(**theHTE).hText);
  1899.                     
  1900.                     SetPort(oldPort);
  1901.                     myErr = CreateOffsetDescriptor(theSize, dataDesc);
  1902.                     
  1903.                     break;
  1904.             
  1905.             case pColor:
  1906.             
  1907.                     myErr = AECreateDesc(typeRGBColor,
  1908.                                                              (Ptr)&theTextStyle.tsColor,
  1909.                                                              sizeof(theTextStyle.tsColor),
  1910.                                                              dataDesc);
  1911.                     break;
  1912.  
  1913.             case pDefaultType:
  1914.             case pBestType:
  1915.             
  1916.                     myErr = MakeTextObj(theTextPropToken.propertyTextToken.tokenWindow,
  1917.                                                          theTextPropToken.propertyTextToken.tokenOffset-1,
  1918.                                                          theTextPropToken.propertyTextToken.tokenOffset+
  1919.                                                          theTextPropToken.propertyTextToken.tokenLength-1,
  1920.                                                          dataDesc);
  1921.                     break;
  1922.  
  1923.             case pClass:
  1924.             
  1925.                     myErr = MakeClassObj(cChar, dataDesc);
  1926.                     break;
  1927.                     
  1928.             default:
  1929.                     myErr = errAEEventNotHandled;
  1930.         }
  1931.         return(myErr);
  1932.         
  1933.     } /*GetTextProperty*/
  1934.     
  1935. /* -----------------------------------------------------------------------
  1936.         Name:             GetWindowProperty
  1937.         Purpose:        Fills dataDesc with the requested window property.
  1938.      -----------------------------------------------------------------------**/
  1939. typedef Rect **RectHandle;
  1940.  
  1941. pascal OSErr GetWindowProperty(const AEDesc *theWPTokenObj, AEDesc *dataDesc)
  1942.   {             
  1943.     OSErr           theErr;
  1944.         OSErr           ignoreErr;
  1945.         Str255          theName;
  1946.         Boolean         theBoolean;
  1947.         Rect            theRect;
  1948.         Point           thePoint;
  1949.         Rect            winRect;
  1950.         Rect            userRect;
  1951.         short           theIndex;
  1952.         DPtr            theDocument;
  1953.         TEHandle        theHTE;
  1954.         windowPropToken theWindowPropToken;
  1955.         AEDesc          newDesc;
  1956.         Size            tokenSize;
  1957.         
  1958.       theErr = AECoerceDesc(theWPTokenObj,typeMyWindowProp, &newDesc);
  1959.         
  1960.         if (theErr == noErr)
  1961.             {
  1962.                 GetRawDataFromDescriptor(&newDesc,
  1963.                                                                  (Ptr)&theWindowPropToken,
  1964.                                                                  sizeof(theWindowPropToken),
  1965.                                                                  &tokenSize);
  1966.                                                                  
  1967.                 ignoreErr = AEDisposeDesc(&newDesc);
  1968.             }
  1969.         else
  1970.           return(theErr);
  1971.             
  1972.         switch (theWindowPropToken.tokenProperty) { 
  1973.             
  1974.             case pName:
  1975.             
  1976.                 GetWTitle(theWindowPropToken.tokenWindowToken, theName);
  1977.                 theErr = AECreateDesc(typeChar,
  1978.                                                             (Ptr)&theName[1],
  1979.                                                             theName[0],
  1980.                                                             dataDesc);
  1981.                 break;
  1982.             
  1983.             case pContents:
  1984.             
  1985.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1986.                 theHTE      = theDocument->theText;
  1987.                 
  1988.                 theErr = BuildStyledTextDesc( theHTE,
  1989.                                                                             1,
  1990.                                                                             (**theHTE).teLength,
  1991.                                                                             dataDesc);
  1992.                 break;
  1993.             
  1994.             case pBounds:
  1995.             
  1996.                 SetPort(theWindowPropToken.tokenWindowToken);
  1997.  
  1998.                 theRect = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox;
  1999.                 
  2000.                 theErr  = AECreateDesc(typeQDRectangle,
  2001.                                                              (Ptr)&theRect,
  2002.                                                              sizeof(theRect),
  2003.                                                              dataDesc);
  2004.                 break;
  2005.             
  2006.             case pPosition:
  2007.             
  2008.                 thePoint.v = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox.top;
  2009.                 thePoint.h = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox.left;
  2010.                             
  2011.                 theErr   = AECreateDesc(typeQDPoint,
  2012.                                                               (Ptr)&thePoint,
  2013.                                                               sizeof(thePoint),
  2014.                                                               dataDesc);
  2015.                 break;
  2016.                         
  2017.             case pVisible:
  2018.             
  2019.                 theBoolean = ((WindowPeek)theWindowPropToken.tokenWindowToken)->visible;
  2020.                                 
  2021.                 theErr  = AECreateDesc(typeBoolean,
  2022.                                                              (Ptr)&theBoolean,
  2023.                                                              sizeof(theBoolean),
  2024.                                                              dataDesc);
  2025.                 break;
  2026.             
  2027.             case pIsModal:
  2028.             
  2029.                 theBoolean = false;
  2030.                                 
  2031.                 theErr  = AECreateDesc(typeBoolean,
  2032.                                                              (Ptr)&theBoolean,
  2033.                                                              sizeof(theBoolean),
  2034.                                                              dataDesc);
  2035.                 break;
  2036.  
  2037.             case pIsZoomed:
  2038.             
  2039.                 if (((WindowPeek)theWindowPropToken.tokenWindowToken)->spareFlag) 
  2040.                     {
  2041.                         SetPort(theWindowPropToken.tokenWindowToken);
  2042.                         
  2043.                         userRect = **((RectHandle)((WindowPeek)qd.thePort)->dataHandle);
  2044.                         winRect  = qd.thePort->portRect;
  2045.                         LocalToGlobal((Point *)&winRect.top);
  2046.                         LocalToGlobal((Point *)&winRect.bottom);
  2047.                         
  2048.                         theBoolean = !EqualRect(&userRect, &winRect);
  2049.                     }
  2050.                 else
  2051.                     theBoolean = false;
  2052.                                 
  2053.                 theErr  = AECreateDesc(typeBoolean,
  2054.                                                              (Ptr)&theBoolean,
  2055.                                                              sizeof(theBoolean),
  2056.                                                              dataDesc);
  2057.                 break;
  2058.             
  2059.             case pIsResizable:
  2060.             case pHasTitleBar:
  2061.             case pHasCloseBox:
  2062.             case pIsZoomable :
  2063.             
  2064.                 theBoolean = true;
  2065.                                 
  2066.                 theErr  = AECreateDesc(typeBoolean,
  2067.                                                              (Ptr)&theBoolean,
  2068.                                                              sizeof(theBoolean),
  2069.                                                              dataDesc);
  2070.                 break;
  2071.         
  2072.             case pIsFloating:
  2073.             
  2074.                 theBoolean = false;
  2075.                                 
  2076.                 theErr  = AECreateDesc(typeBoolean,
  2077.                                                              (Ptr)&theBoolean,
  2078.                                                              sizeof(theBoolean),
  2079.                                                              dataDesc);
  2080.                 break;
  2081.             
  2082.             case pIsModified:
  2083.             
  2084.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2085.                 
  2086.                 theBoolean  = theDocument->dirty;
  2087.                 
  2088.                 theErr  = AECreateDesc(typeBoolean,
  2089.                                                              (Ptr)&theBoolean,
  2090.                                                              sizeof(theBoolean),
  2091.                                                              dataDesc);
  2092.                 break;
  2093.             
  2094.             case pIndex:
  2095.             
  2096.                 theIndex = 0;
  2097.                 if (theWindowPropToken.tokenWindowToken) 
  2098.                     do
  2099.                         theIndex++;
  2100.                     while (theWindowPropToken.tokenWindowToken != GetWindowPtrOfNthWindow(theIndex));
  2101.                                 
  2102.                 theErr  = AECreateDesc(typeShortInteger,
  2103.                                                              (Ptr)&theIndex,
  2104.                                                              sizeof(theIndex),
  2105.                                                              dataDesc);
  2106.                 break;
  2107.             
  2108.             case pPageSetup:
  2109.             
  2110.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2111.                 
  2112.                 HLock((Handle)theDocument->thePrintSetup);
  2113.                 
  2114.                 theErr  = AECreateDesc(typeTPrint,
  2115.                                                              (Ptr)*(theDocument->thePrintSetup),
  2116.                                                              sizeof(TPrint),
  2117.                                                              dataDesc);
  2118.                                                              
  2119.                 HUnlock((Handle)theDocument->thePrintSetup);
  2120.                                                              
  2121.                 break;
  2122.             
  2123.             case pSelection:
  2124.             
  2125.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2126.                 
  2127.                 theErr  = MakeSelTextObj( theWindowPropToken.tokenWindowToken,
  2128.                                                                     theDocument->theText, 
  2129.                                                                     dataDesc);                                                             
  2130.                 break;
  2131.             
  2132.             case pDefaultType:
  2133.             case pBestType:
  2134.             
  2135.                 theErr  = MakeWindowObj(theWindowPropToken.tokenWindowToken,
  2136.                                                                 dataDesc);                                                             
  2137.                 break;
  2138.             
  2139.             case pClass:
  2140.             
  2141.                 theErr  = MakeClassObj(cWindow, dataDesc);                                                             
  2142.                 break;
  2143.                 
  2144.         default:
  2145.                 theErr = errAEEventNotHandled;
  2146.         }
  2147.         return(theErr);
  2148.     } /* GetWindowProperty */
  2149.     
  2150. /** -----------------------------------------------------------------------
  2151.         Name:             GetApplicationProperty
  2152.         Purpose:        Fills dataDesc with the requested application property.
  2153.      -----------------------------------------------------------------------**/
  2154.      
  2155. pascal OSErr GetApplicationProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  2156.     {
  2157.       OSErr         theErr;
  2158.         OSErr         ignoreErr;
  2159.         Str255        theName;
  2160.         Boolean       isFront;
  2161.         applPropToken theApplPropToken;
  2162.         AEDesc        newDesc;
  2163.         Size          tokenSize;
  2164.         
  2165.         theErr = AECoerceDesc(theObjToken, typeMyApplProp, &newDesc);
  2166.         if (theErr==noErr)
  2167.             {
  2168.                 GetRawDataFromDescriptor(&newDesc,
  2169.                                                                  (Ptr)&theApplPropToken,
  2170.                                                                  sizeof(theApplPropToken),
  2171.                                                                  &tokenSize);
  2172.                                                                  
  2173.                 ignoreErr = AEDisposeDesc(&newDesc);
  2174.             }
  2175.         else
  2176.           return(theErr);
  2177.  
  2178.         theErr = kAEGenericErr;
  2179.         
  2180.         switch (theApplPropToken.tokenApplProperty) {
  2181.         
  2182.             case pName:
  2183.             
  2184.                     PLstrcpy((StringPtr)theName, (unsigned char *)"\pMenuScripter");
  2185.                     theErr  = AECreateDesc(typeChar,
  2186.                                                                  (Ptr)&theName[1],
  2187.                                                                  theName[0],
  2188.                                                                  dataDesc);
  2189.                     break;
  2190.             
  2191.             case pVersion:
  2192.             
  2193.                     PLstrcpy((StringPtr)theName, (unsigned char *)"\p1.0d4");
  2194.                     theErr  = AECreateDesc(typeChar,
  2195.                                                                  (Ptr)&theName[1],
  2196.                                                                  theName[0],
  2197.                                                                  dataDesc);
  2198.                     break;
  2199.         
  2200.             case pIsFrontProcess:
  2201.             
  2202.                     isFront = !gInBackground;
  2203.                     theErr  = AECreateDesc(typeBoolean,
  2204.                                                                  (Ptr)&isFront,
  2205.                                                                  sizeof(isFront),
  2206.                                                                  dataDesc);
  2207.                     break;
  2208.         
  2209.             case pDefaultType:
  2210.             case pBestType:
  2211.             
  2212.                     theErr  = AECreateDesc(typeNull,
  2213.                                                                  nil,
  2214.                                                                  0,
  2215.                                                                  dataDesc);
  2216.                     break;
  2217.             
  2218.             case pClass: 
  2219.             
  2220.                     theErr  = MakeClassObj(cApplication, dataDesc);
  2221.                     break;
  2222.             
  2223.             default:
  2224.             
  2225.                     theErr = errAEEventNotHandled;
  2226.         }
  2227.         
  2228.         return(theErr);
  2229.         
  2230.     } /* GetApplicationProperty */
  2231.      
  2232. /** -----------------------------------------------------------------------
  2233.         Name:             GetMenuProperty
  2234.         Purpose:        Fills dataDesc with the requested menu property.
  2235.      -----------------------------------------------------------------------**/
  2236.      
  2237. pascal OSErr GetMenuProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  2238.     {
  2239.       OSErr         theErr;
  2240.         OSErr         ignoreErr;
  2241.         Str255        theName;
  2242.         MenuPropToken theMenuPropToken;
  2243.         AEDesc        newDesc;
  2244.         Size          tokenSize;
  2245.         short         activeItem;
  2246.         
  2247.         theErr = AECoerceDesc(theObjToken, typeMyMenuProp, &newDesc);
  2248.         if (theErr==noErr)
  2249.             {
  2250.                 GetRawDataFromDescriptor(&newDesc,
  2251.                                                                  (Ptr)&theMenuPropToken,
  2252.                                                                  sizeof(theMenuPropToken),
  2253.                                                                  &tokenSize);
  2254.                                                                  
  2255.                 ignoreErr = AEDisposeDesc(&newDesc);
  2256.             }
  2257.         else
  2258.           return(theErr);
  2259.  
  2260.         switch (theMenuPropToken.theMenuProp) {
  2261.             case pName : 
  2262.             
  2263.                     PLstrcpy(theName, (**theMenuPropToken.theMenuToken.theTokenMenu).menuData);
  2264.                     theErr  = AECreateDesc(typeChar,
  2265.                                                                  (Ptr)&theName[1],
  2266.                                                                  theName[0],
  2267.                                                                  dataDesc);
  2268.                     break;
  2269.                 
  2270.             case pMenuID:
  2271.             
  2272.                     theErr  = AECreateDesc(typeShortInteger,
  2273.                                                                  (Ptr)&theMenuPropToken.theMenuToken.theTokenID,
  2274.                                                                  sizeof(theMenuPropToken.theMenuToken.theTokenID),
  2275.                                                                  dataDesc);
  2276.                     break;
  2277.                     
  2278.             case pActiveItem:
  2279.             
  2280.                     activeItem = GetScriptActiveItem();
  2281.                 
  2282.                     theErr  = AECreateDesc(typeShortInteger,
  2283.                                                                  (Ptr)&activeItem,
  2284.                                                                  sizeof(activeItem),
  2285.                                                                  dataDesc);
  2286.                     break;    
  2287.             
  2288.             case pDefaultType:
  2289.             case pBestType:
  2290.             
  2291.                     PLstrcpy(theName, (**theMenuPropToken.theMenuToken.theTokenMenu).menuData);
  2292.                     
  2293.                     theErr  = MakeMenuObj(theName, dataDesc);
  2294.                     break;
  2295.             
  2296.             case pClass:
  2297.             
  2298.                     theErr  = MakeClassObj(cMenu, dataDesc);
  2299.                     
  2300.                     break;
  2301.                     
  2302.          default:
  2303.                     theErr = errAEEventNotHandled;
  2304.         }
  2305.         
  2306.         return(theErr);
  2307.     } /* GetMenuProperty */
  2308.     
  2309. /** -----------------------------------------------------------------------
  2310.         Name:             GetMenuItemProperty
  2311.         Purpose:        Fills dataDesc with the requested menu property.
  2312.      -----------------------------------------------------------------------**/
  2313.      
  2314. pascal OSErr GetMenuItemProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  2315.     {
  2316.       OSErr             theErr;
  2317.         OSErr             ignoreErr;
  2318.         Str255            theName;
  2319.         MenuItemPropToken theMenuItemPropToken;
  2320.         AEDesc            newDesc;
  2321.         Size              tokenSize;
  2322.         
  2323.         theErr = AECoerceDesc(theObjToken, typeMyItemProp, &newDesc);
  2324.         if (theErr==noErr)
  2325.             {
  2326.                 GetRawDataFromDescriptor(&newDesc,
  2327.                                                                  (Ptr)&theMenuItemPropToken,
  2328.                                                                  sizeof(theMenuItemPropToken),
  2329.                                                                  &tokenSize);
  2330.                                                                  
  2331.                 ignoreErr = AEDisposeDesc(&newDesc);
  2332.             }
  2333.         else
  2334.           return(theErr);
  2335.  
  2336.         switch (theMenuItemPropToken.theItemProp) {
  2337.         
  2338.             case pName :
  2339.                             GetItem(theMenuItemPropToken.theItemToken.theMenuToken.theTokenMenu,
  2340.                                             theMenuItemPropToken.theItemToken.theTokenItem,
  2341.                                             theName);
  2342.                             theErr  = AECreateDesc(typeChar,
  2343.                                                                          (Ptr)&theName[1],
  2344.                                                                          theName[0],
  2345.                                                                          dataDesc);
  2346.                             break;
  2347.             
  2348.             case pItemNumber:
  2349.         
  2350.                             theErr  = AECreateDesc(typeShortInteger,
  2351.                                                                          (Ptr)&theMenuItemPropToken.theItemToken.theTokenItem,
  2352.                                                                          sizeof(theMenuItemPropToken.theItemToken.theTokenItem),
  2353.                                                                          dataDesc);
  2354.                             break;
  2355.                             
  2356.             case pDefaultType:
  2357.             case pBestType:
  2358.                             
  2359.                             PLstrcpy(theName, (**theMenuItemPropToken.theItemToken.theMenuToken.theTokenMenu).menuData);
  2360.                             
  2361.                             theErr  = MakeMenuItemObj(theName,
  2362.                                                                                 theMenuItemPropToken.theItemToken.theTokenItem,
  2363.                                                                                 dataDesc);
  2364.                             break;
  2365.                             
  2366.             case pClass:
  2367.                             
  2368.                             theErr  = MakeClassObj(cMenuItem, dataDesc);
  2369.                             break;
  2370.                             
  2371.             default: 
  2372.                             theErr = errAEEventNotHandled;
  2373.                             
  2374.         }
  2375.             
  2376.         return(theErr);
  2377.     } /* GetMenuItemProperty */
  2378.  
  2379. /** -----------------------------------------------------------------------
  2380.         Name:             HandleGetData
  2381.         Purpose:        Coerces theObj into a token which we understand and
  2382.                                 extracts the data requested in the token and puts it
  2383.                                 into dataDesc.
  2384.      -----------------------------------------------------------------------**/
  2385.      
  2386. typedef char chars[32001];
  2387. typedef chars **charsHandle;
  2388.  
  2389.  
  2390. pascal OSErr HandleGetData(AEDesc *theObj, DescType whatType, AEDesc *dataDesc)
  2391.   {
  2392. #pragma unused (whatType)
  2393.  
  2394.       OSErr           myErr;
  2395.         OSErr                        ignoreErr;
  2396.         AEDesc          newDesc;
  2397.         TextToken       theTextToken;
  2398.         Size            tokenSize;
  2399.         DPtr            theDoc;
  2400.         AEDesc          objTokenDesc;
  2401.         
  2402.         /*
  2403.             Resolve theObj into a token which we can use - 
  2404.                  get the property or data for that token
  2405.         */    
  2406.     
  2407.         myErr = AEResolve(theObj ,kAEIDoMinimum, &objTokenDesc);
  2408.         
  2409.         if (myErr == noErr)    
  2410.             switch (objTokenDesc.descriptorType){
  2411.             
  2412.                 case typeMyApplProp : myErr = GetApplicationProperty(&objTokenDesc, dataDesc);
  2413.                                                             break;
  2414.                                                             
  2415.                 case typeMyMenuProp : myErr = GetMenuProperty(&objTokenDesc, dataDesc);
  2416.                                                             break;
  2417.                 
  2418.                 case typeMyItemProp : myErr = GetMenuItemProperty(&objTokenDesc, dataDesc);
  2419.                                                             break;
  2420.                 
  2421.                 case typeMyTextProp : myErr = GetTextProperty(&objTokenDesc, dataDesc);
  2422.                                                             break;
  2423.                 
  2424.                 case typeMyWindowProp:myErr = GetWindowProperty(&objTokenDesc, dataDesc);
  2425.                                                             break;
  2426.                 
  2427.                 case typeMyText     :if (AECoerceDesc(&objTokenDesc, typeMyText, &newDesc) == noErr) 
  2428.                                                             {
  2429.                                                                 GetRawDataFromDescriptor(&newDesc,
  2430.                                                                                                                  (Ptr)&theTextToken,
  2431.                                                                                                                  sizeof(theTextToken),
  2432.                                                                                                                  &tokenSize);
  2433.                                                                 
  2434.                                                                 ignoreErr = AEDisposeDesc(&newDesc);
  2435.                                                                                                                  
  2436.                                                                 theDoc = DPtrFromWindowPtr(theTextToken.tokenWindow);
  2437.                                                                 
  2438.                                                                 myErr = BuildStyledTextDesc(theDoc->theText,
  2439.                                                                                                                         theTextToken.tokenOffset,
  2440.                                                                                                                         theTextToken.tokenLength,
  2441.                                                                                                                         dataDesc);
  2442.                                             
  2443.                                                             }
  2444.                                                          break;
  2445.                                                          
  2446.                 default : myErr = errAEWrongDataType;
  2447.             }
  2448.         return(myErr);
  2449.     }    /* HandleGetData */
  2450.  
  2451. /** -----------------------------------------------------------------------
  2452.         Name:             CheckSender
  2453.         Purpose:        Finds the Sender of the AppleEvent.
  2454.      -----------------------------------------------------------------------**/
  2455.      
  2456. pascal void CheckSender(const AppleEvent *theAppleEvent)
  2457.     {
  2458.         OSErr    myErr;
  2459.         TargetID sender;
  2460.         DescType returnedType;
  2461.         Size     actSize;
  2462.         
  2463.         myErr = AEGetAttributePtr(theAppleEvent,
  2464.                                                             keyAddressAttr,
  2465.                                                             typeTargetID,
  2466.                                                             &returnedType,
  2467.                                                             (Ptr)&sender,
  2468.                                                             sizeof(sender),
  2469.                                                             &actSize);
  2470.                                                             
  2471.         // DebugStr((Str255)sender.name.name);
  2472.     }
  2473.  
  2474. /** -----------------------------------------------------------------------
  2475.         Name:             DoGetData
  2476.         Purpose:        Handles the GetData AppleEvent.
  2477.      -----------------------------------------------------------------------**/
  2478.  
  2479. pascal OSErr DoGetData(const AppleEvent *theAppleEvent,
  2480.                                          AppleEvent *reply,
  2481.                                          long handlerRefCon)
  2482.     { 
  2483. #pragma unused (handlerRefCon)
  2484.  
  2485.         OSErr    myErr;
  2486.         OSErr    tempErr;
  2487.         AEDesc   myDirObj;
  2488.         AEDesc   myDataDesc;
  2489.         Size     actualSize;
  2490.         DescType returnedType;
  2491.         DescType reqType;
  2492.         
  2493.         myDataDesc.dataHandle = nil;
  2494.         myDirObj.dataHandle   = nil;
  2495.         
  2496.         /*
  2497.             CheckSender(theAppleEvent);
  2498.         */
  2499.         
  2500.         /*
  2501.             extract the direct object, which is the object whose data is to be returned
  2502.         */
  2503.         
  2504.         myErr  = AEGetParamDesc(theAppleEvent,
  2505.                                                         keyDirectObject,
  2506.                                                         typeWildCard,
  2507.                                                         &myDirObj);
  2508.             
  2509.         /*
  2510.             now the get the type of data wanted - optional
  2511.         */
  2512.         
  2513.         tempErr = AEGetParamPtr(theAppleEvent,
  2514.                                                         keyAERequestedType,
  2515.                                                         typeType,
  2516.                                                         &returnedType,
  2517.                                                         (Ptr)&reqType,
  2518.                                                         sizeof(reqType),
  2519.                                                         &actualSize);
  2520.         if (tempErr!=noErr) 
  2521.             reqType = typeChar;
  2522.             
  2523.         if (myErr == noErr) 
  2524.             myErr = GotRequiredParams(theAppleEvent);
  2525.         
  2526.         /* get the data */
  2527.         if (myErr == noErr) 
  2528.             myErr = HandleGetData(&myDirObj, reqType, &myDataDesc);
  2529.             
  2530.         /* if they wanted a reply, attach it now */
  2531.         if (myErr==noErr) 
  2532.             if (reply->descriptorType != typeNull) 
  2533.                 myErr = AEPutParamDesc(reply, keyDirectObject, &myDataDesc);
  2534.                 
  2535.     if (myDataDesc.dataHandle)
  2536.           tempErr = AEDisposeDesc(&myDataDesc);
  2537.  
  2538.     if (myDirObj.dataHandle)
  2539.           tempErr = AEDisposeDesc(&myDirObj);
  2540.             
  2541.         return(myErr);
  2542.     }    /* DoGetData */
  2543.  
  2544.  
  2545. /** -----------------------------------------------------------------------
  2546.         Name:             DoGetDataSize
  2547.         Purpose:        Handles the GetDataSize AppleEvent.
  2548.      -----------------------------------------------------------------------**/
  2549.  
  2550. pascal OSErr DoGetDataSize(const AppleEvent *theAppleEvent,
  2551.                                                         AppleEvent *reply,
  2552.                                                                  long       handlerRefCon)
  2553.   { 
  2554. #pragma unused (handlerRefCon)
  2555.     
  2556.         OSErr     myErr;
  2557.         OSErr     tempErr;
  2558.         AEDesc    myDirObj;
  2559.         AEDesc    myDataDesc;
  2560.         Size      actualSize;
  2561.         DescType  returnedType;
  2562.         DescType  reqType;
  2563.         long      dataSize;
  2564.         
  2565.         myDataDesc.dataHandle = nil;
  2566.         myDirObj.dataHandle   = nil;
  2567.         
  2568.         /* pick up the direct object, which is the object whose data is to be sized */
  2569.         
  2570.         myErr  = AEGetParamDesc(theAppleEvent,
  2571.                                                         keyDirectObject,
  2572.                                                         typeWildCard,
  2573.                                                         &myDirObj);
  2574.             
  2575.         /* now the get the type wanted - optional*/
  2576.         
  2577.         tempErr = AEGetParamPtr(theAppleEvent,
  2578.                                                         keyAERequestedType,
  2579.                                                         typeType,
  2580.                                                         &returnedType,
  2581.                                                         (Ptr)&reqType,
  2582.                                                         sizeof(reqType),
  2583.                                                         &actualSize);
  2584.         
  2585.         if (tempErr!=noErr) 
  2586.             reqType = typeChar;
  2587.             
  2588.         if (myErr == noErr) 
  2589.             myErr = GotRequiredParams(theAppleEvent);
  2590.         
  2591.         /* get the data */
  2592.         if (myErr == noErr) 
  2593.             myErr = HandleGetData(&myDirObj, reqType, &myDataDesc);
  2594.             
  2595.         /* evaluate size of data and discard, create desc for size */
  2596.         if (myErr == noErr) 
  2597.             if (myDataDesc.dataHandle) 
  2598.                 {
  2599.                     dataSize = GetHandleSize((Handle)myDataDesc.dataHandle);
  2600.                     DisposHandle((Handle)myDataDesc.dataHandle);
  2601.                     myErr  = AECreateDesc(typeLongInteger,
  2602.                                                                 (Ptr)&dataSize,
  2603.                                                                 sizeof(dataSize),
  2604.                                                                 &myDataDesc);
  2605.                 }
  2606.             
  2607.             
  2608.         /* if they wanted a reply, attach it now */
  2609.         
  2610.         if (myErr==noErr) 
  2611.             if (reply->descriptorType != typeNull) 
  2612.                 myErr = AEPutParamDesc(reply, keyDirectObject, &myDataDesc);
  2613.                 
  2614.         /* discard our copy */
  2615.         
  2616.         if (myDataDesc.dataHandle) 
  2617.             tempErr = AEDisposeDesc(&myDataDesc);
  2618.                 
  2619.         if (myDirObj.dataHandle) 
  2620.             tempErr = AEDisposeDesc(&myDirObj);
  2621.                 
  2622.         return(myErr);
  2623.     }    /* DoGetDataSize */
  2624.  
  2625. /** -----------------------------------------------------------------------
  2626.         Name:             DoNewElement
  2627.         Purpose:        Handles the NewElement AppleEvent. Only Creates windows for
  2628.                     now.
  2629.      -----------------------------------------------------------------------**/
  2630.  
  2631. pascal OSErr DoNewElement(const AppleEvent *theAppleEvent,
  2632.                                                 AppleEvent *reply, 
  2633.                                                 long       handlerRefCon)
  2634. {
  2635. #pragma unused (handlerRefCon)
  2636.  
  2637.     OSErr       myErr;
  2638.     OSErr       ignoreErr;
  2639.     DescType      returnedType;
  2640.     DescType      newElemClass;
  2641.     Size        actSize;
  2642.     AEDesc        wndwObjSpec;
  2643.     DPtr        theDoc;
  2644.     
  2645.     wndwObjSpec.dataHandle = nil;
  2646.         
  2647.     myErr = AEGetParamPtr(theAppleEvent,
  2648.                                                 keyAEObjectClass,
  2649.                                                 typeType,
  2650.                                                 &returnedType,
  2651.                                                 (Ptr)&newElemClass,
  2652.                                                 sizeof(newElemClass),
  2653.                                                 &actSize);
  2654.             
  2655.   /* check for missing required parameters */
  2656.     
  2657.   if (myErr == noErr) 
  2658.         myErr = GotRequiredParams(theAppleEvent);
  2659.   
  2660.   /* got all required params */
  2661.   
  2662.   /* let's make sure container is the null desc */
  2663.   /* and they want a window */
  2664.     
  2665.   if (newElemClass != cWindow) 
  2666.     myErr = errAEWrongDataType;
  2667.   
  2668.   /* let's create a new window */
  2669.     
  2670.     if (myErr == noErr) 
  2671.         theDoc = NewDocument(false);
  2672.   
  2673.     if (myErr==noErr)
  2674.         if (theDoc == nil) 
  2675.             myErr = -1700;
  2676.         else
  2677.             {
  2678.                 ShowWindow(theDoc->theWindow);
  2679.                 theDoc->dirty = false;
  2680.             
  2681.                 myErr = MakeWindowObj(theDoc->theWindow, &wndwObjSpec);
  2682.             }
  2683.       
  2684.     if (myErr == noErr) 
  2685.         if (reply->descriptorType != typeNull) 
  2686.              myErr = AEPutParamDesc(reply,
  2687.                                                             keyDirectObject,
  2688.                                                             &wndwObjSpec);
  2689.             
  2690.     if (wndwObjSpec.dataHandle) 
  2691.         ignoreErr = AEDisposeDesc(&wndwObjSpec);
  2692.         
  2693.   return(myErr);
  2694. }    /* DoNewElement */
  2695.  
  2696. /** -----------------------------------------------------------------------
  2697.         Name:             DoIsThereA
  2698.         Purpose:        Handles the IsThereA AppleEvent.
  2699.      -----------------------------------------------------------------------**/
  2700.  
  2701. pascal OSErr DoIsThereA(const AppleEvent *theAppleEvent,
  2702.                                             AppleEvent       *reply, 
  2703.                                             long             handlerRefCon)
  2704.                                             
  2705. /*
  2706.     Support check of Windows at first
  2707.     
  2708.     What we do :
  2709.         Get Direct Object
  2710.         Check have all required params
  2711.         Coerce into things we support
  2712.         if we get something back
  2713.             check to see it exists and set reply
  2714.         clean up
  2715.         return
  2716. */
  2717.  
  2718.  {
  2719. #pragma unused (handlerRefCon)
  2720.  
  2721.         OSErr         myErr;
  2722.         OSErr         ignoreErr;
  2723.         AEDesc        myDirObject;
  2724.         AEDesc        windDesc;
  2725.         AEDesc        dataDesc;
  2726.         WindowToken   theWindowToken;
  2727.         Size          tokenSize;
  2728.         Boolean       exists;
  2729.  
  2730.         myDirObject.dataHandle = nil;
  2731.         windDesc.dataHandle    = nil;
  2732.         dataDesc.dataHandle    = nil;
  2733.                 
  2734.         myErr = AEGetParamDesc(theAppleEvent,
  2735.                                                      keyDirectObject,
  2736.                                                      typeWildCard,
  2737.                                                      &myDirObject);
  2738.                         
  2739.         /* check for missing required parameters */
  2740.         
  2741.         if (myErr == noErr) 
  2742.             myErr = GotRequiredParams(theAppleEvent);
  2743.         
  2744.         /* got all required params */
  2745.         
  2746.         /* let's make sure they want to check for a window */
  2747.             
  2748.         exists = false;
  2749.         
  2750.         if (myErr == noErr) 
  2751.             if (AECoerceDesc(&myDirObject, typeMyWndw, &windDesc)==noErr) 
  2752.                 if (windDesc.descriptorType!=typeNull) 
  2753.                     {
  2754.                         GetRawDataFromDescriptor(&windDesc,
  2755.                                                                          (Ptr)&theWindowToken,
  2756.                                                                          sizeof(theWindowToken),
  2757.                                                                          &tokenSize);
  2758.                                                                          
  2759.                         exists = (theWindowToken != nil);    
  2760.                     }
  2761.                     
  2762.         if (myErr == noErr) 
  2763.             myErr = AECreateDesc(typeBoolean,
  2764.                                                      (Ptr)&exists,
  2765.                                                      sizeof(exists),
  2766.                                                      &dataDesc);
  2767.     
  2768.             
  2769.         /* 
  2770.             if they wanted a reply, which they surely must, 
  2771.             attach the result to it…
  2772.         */
  2773.             
  2774.         if (myErr == noErr)
  2775.             if (reply->descriptorType != typeNull) 
  2776.                  myErr = AEPutParamDesc(reply,
  2777.                                                                 keyDirectObject,
  2778.                                                                 &dataDesc);
  2779.                 
  2780.         if (dataDesc.dataHandle) 
  2781.             ignoreErr = AEDisposeDesc(&dataDesc);
  2782.             
  2783.         if (myDirObject.dataHandle) 
  2784.             ignoreErr = AEDisposeDesc(&myDirObject);
  2785.             
  2786.         if (windDesc.dataHandle) 
  2787.             ignoreErr = AEDisposeDesc(&windDesc);
  2788.             
  2789.         return(myErr);
  2790.     }    /* DoIsThereA */
  2791.  
  2792. /** -----------------------------------------------------------------------
  2793.         Name:             DoCloseWindow
  2794.         Purpose:        Handles the Close AppleEvent.
  2795.      -----------------------------------------------------------------------**/
  2796.  
  2797. pascal OSErr DoCloseWindow(const AppleEvent *theAppleEvent,
  2798.                                                     AppleEvent       *reply,
  2799.                                                     long             handlerRefCon)
  2800.     { 
  2801. #pragma unused (reply, handlerRefCon)
  2802.  
  2803.         OSErr         myErr;
  2804.         OSErr         tempErr;
  2805.         OSErr                 ignoreErr;
  2806.         AEDesc        myDirObj;
  2807.         AEDesc        newDesc;
  2808.         WindowToken   theWindowToken;
  2809.         Size          tokenSize;
  2810.         DescType      saveOpt;
  2811.         Size          actSize;
  2812.         DescType      returnedType;
  2813.         DPtr          myDPtr;
  2814.  
  2815.         myDirObj.dataHandle = nil;
  2816.         
  2817.         /* pick up the direct object, which is the object (window) to close */
  2818.         
  2819.         myErr = AEGetParamDesc(theAppleEvent,
  2820.                                                      keyDirectObject,
  2821.                                                      typeWildCard,
  2822.                                                      &myDirObj);
  2823.                     
  2824.         /* pick up optional save param, if any */
  2825.         
  2826.         saveOpt = kAEAsk; /* the default */
  2827.         
  2828.         tempErr = AEGetParamPtr(theAppleEvent,
  2829.                                                         keyAESaveOptions,
  2830.                                                         typeEnumerated,
  2831.                                                         &returnedType,
  2832.                                                         (Ptr)&saveOpt,
  2833.                                                         sizeof(saveOpt),
  2834.                                                         &actSize);
  2835.                                                          
  2836.         if (myErr == noErr)
  2837.             myErr = GotRequiredParams(theAppleEvent);
  2838.             
  2839.         /* get the window to close as a window ptr */
  2840.         if (myErr == noErr) 
  2841.             if (AECoerceDesc(&myDirObj, typeMyWndw, &newDesc)==noErr) 
  2842.                 if (newDesc.descriptorType!=typeNull) 
  2843.                     {
  2844.                         GetRawDataFromDescriptor(&newDesc,
  2845.                                                                          (Ptr)&theWindowToken,
  2846.                                                                          sizeof(theWindowToken),
  2847.                                                                          &tokenSize);
  2848.                                                                          
  2849.                         ignoreErr = AEDisposeDesc(&newDesc);
  2850.                         
  2851.                         if (theWindowToken) 
  2852.                             {
  2853.                                 myErr=AESetInteractionAllowed(kAEInteractWithAll); /* Should do this in prefs */
  2854.                                 
  2855.                                 /*
  2856.                                     We do some of the close checks here to avoid
  2857.                                     calling AEInteractWithUser
  2858.                                 */
  2859.                                 myDPtr = DPtrFromWindowPtr(theWindowToken);
  2860.                                 if ((myDPtr->dirty) || (myDPtr->everSaved == false)) 
  2861.                                     if (saveOpt != kAENo) /* Don't flip layers if force no ask */
  2862.                                         myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  2863.                                     
  2864.                                 if (myErr==noErr) 
  2865.                                     myErr = DoClose(theWindowToken, true, saveOpt);
  2866.                             }
  2867.                         else
  2868.                             myErr =  errAEIllegalIndex;
  2869.                     }
  2870.                         
  2871.         if (myDirObj.dataHandle) 
  2872.             tempErr = AEDisposeDesc(&myDirObj);
  2873.                 
  2874.         return(myErr);
  2875.     }    /* DoCloseWindow */
  2876.  
  2877. /** -----------------------------------------------------------------------
  2878.         Name:             IndexFromWindowPtr
  2879.         Purpose:        calculates index of WindowPtr.
  2880.                     1 == frontmost window
  2881.                                 0 == no window
  2882.                                 n == n-1 behind first window
  2883.      -----------------------------------------------------------------------**/
  2884.  
  2885. pascal short IndexFromWindowPtr(WindowPtr theWindowPtr)
  2886.  {
  2887.          short     index;
  2888.         WindowPtr checkWin;
  2889.         
  2890.         index = 0;
  2891.         checkWin = (WindowPtr)LMGetWindowList();
  2892.          
  2893.         //    iterate through windows - we use WindowList to see all windows visible or not
  2894.         
  2895.         while (checkWin)
  2896.             {
  2897.                 index++;
  2898.                 
  2899.                 if (checkWin==theWindowPtr)
  2900.                     return(index);
  2901.                     
  2902.               checkWin = (WindowPtr)((WindowPeek)checkWin)->nextWindow;
  2903.             }
  2904.             
  2905.         return(0);
  2906.  }
  2907.  
  2908. /** -----------------------------------------------------------------------
  2909.         Name:             MoveWindowRelative
  2910.         Purpose:        Moves one window relative to another.
  2911.      -----------------------------------------------------------------------**/
  2912.  
  2913. pascal OSErr MoveWindowRelative(WindowToken theWindowToken,
  2914.                                 WindowToken theRelWindowToken,
  2915.                                                               DescType    theRelPosn)
  2916.     {
  2917.         short win1Index;
  2918.         short win2Index;
  2919.         OSErr myErr = noErr;
  2920.         
  2921.         win1Index = IndexFromWindowPtr(theWindowToken);
  2922.         win2Index = IndexFromWindowPtr(theRelWindowToken);
  2923.         
  2924.         if (theRelPosn==kAEBefore)
  2925.             if (win2Index==1)
  2926.                 {
  2927.                     SelectWindow(theWindowToken); // special case - make frontmost window
  2928.                     return(myErr);
  2929.                 }
  2930.             else
  2931.                 { // in front of 3 == behind 2 so…
  2932.                     win2Index--;
  2933.                 }
  2934.         
  2935.         if (win2Index!=win1Index+1)
  2936.             {
  2937.                 SendBehind(theWindowToken,theRelWindowToken);
  2938.                 
  2939.                 if (win1Index > win2Index + 1)
  2940.                      // Fix up visRgn as per Inside Mac I-286.
  2941.                     {    
  2942. //                        PaintOne((WindowPeek)theWindowToken,
  2943. //                                         ((WindowPeek)theWindowToken)->strucRgn);
  2944. //                        CalcVis((WindowPeek)theWindowToken);
  2945.                     }
  2946.             }
  2947.         return(myErr);
  2948.     }
  2949.  
  2950. /** -----------------------------------------------------------------------
  2951.         Name:             DoMoveWindow
  2952.         Purpose:        Handles the Move AppleEvent for windows.
  2953.      -----------------------------------------------------------------------**/
  2954.  
  2955. pascal OSErr DoMoveWindow(const AppleEvent *theAppleEvent,
  2956.                                                    AppleEvent       *reply,
  2957.                                                    long             handlerRefCon)
  2958.     { 
  2959. #pragma unused (reply, handlerRefCon)
  2960.  
  2961.         OSErr         myErr;
  2962.         OSErr                 ignoreErr;
  2963.         AEDesc        whereToPutIt;
  2964.         AEDesc        theLocationRec;
  2965.         DescType      theRelPosn;
  2966.         WindowToken   theWindowToken;
  2967.         WindowToken   theRefWindowToken;
  2968.         Size          actSize;
  2969.         DescType      returnedType;
  2970.  
  2971.         whereToPutIt.dataHandle   = nil;
  2972.         theLocationRec.dataHandle = nil;
  2973.         
  2974.         /* pick up the direct object, which is the object (window) to move */
  2975.         
  2976.         myErr = AEGetParamPtr(theAppleEvent,
  2977.                                                     keyDirectObject,
  2978.                                                     typeMyWndw,
  2979.                                                     &returnedType,
  2980.                                                     (Ptr)&theWindowToken,
  2981.                                                     sizeof(theWindowToken),
  2982.                                                     &actSize);
  2983.                     
  2984.         /* pick up where to information */
  2985.         
  2986.         if (myErr==noErr)
  2987.             myErr = AEGetParamDesc(theAppleEvent,
  2988.                                                          keyAEInsertHere,
  2989.                                                          typeInsertionLoc,
  2990.                                                          &whereToPutIt);
  2991.                                                          
  2992.         if (myErr==noErr)
  2993.             {
  2994.                 myErr = AECoerceDesc(&whereToPutIt, typeAERecord, &theLocationRec);
  2995.                                 
  2996.                 if (myErr==noErr)
  2997.                     myErr = AEGetKeyPtr(&theLocationRec,
  2998.                                                             keyAEObject,
  2999.                                                             typeMyWndw,
  3000.                                                             &returnedType,
  3001.                                                             (Ptr)&theRefWindowToken,
  3002.                                                             sizeof(theRefWindowToken),
  3003.                                                             &actSize);
  3004.                 if (myErr==noErr)
  3005.                     myErr = AEGetKeyPtr(&theLocationRec,
  3006.                                                             keyAEPosition,
  3007.                                                             typeEnumerated,
  3008.                                                             &returnedType,
  3009.                                                             (Ptr)&theRelPosn,
  3010.                                                             sizeof(theRelPosn),
  3011.                                                             &actSize);
  3012.                                                             
  3013.                 if (theLocationRec.dataHandle)
  3014.                     ignoreErr = AEDisposeDesc(&theLocationRec);
  3015.             }
  3016.                                          
  3017.         if (myErr == noErr)
  3018.             myErr = GotRequiredParams(theAppleEvent);
  3019.  
  3020.         if (theRelPosn==kAEBefore ||
  3021.             theRelPosn==kAEAfter)
  3022.             MoveWindowRelative(theWindowToken, theRefWindowToken, theRelPosn);
  3023.         else
  3024.             myErr = errAEEventNotHandled;
  3025.             
  3026.         if (whereToPutIt.dataHandle)
  3027.             ignoreErr = AEDisposeDesc(&whereToPutIt);
  3028.                     
  3029.         // Now place one window relative to the other
  3030.         
  3031.         return(myErr);
  3032.     }    /* DoMoveWindow */
  3033.  
  3034.  
  3035. /** -----------------------------------------------------------------------
  3036.         Name:             DoSaveWindow
  3037.         Purpose:        Handles the Save AppleEvent.
  3038.      -----------------------------------------------------------------------**/
  3039.  
  3040. pascal OSErr DoSaveWindow(const AppleEvent *theAppleEvent,
  3041.                                                  AppleEvent       *reply,
  3042.                                                  long             handlerRefCon)
  3043.     { 
  3044. #pragma unused (reply, handlerRefCon)
  3045.     
  3046.         OSErr         myErr;
  3047.         OSErr         tempErr;
  3048.         OSErr         ignoreErr;
  3049.         AEDesc        myDirObj;
  3050.         AEDesc        newDesc;
  3051.         WindowToken   theWindowToken;
  3052.         Size          tokenSize;
  3053.         Size          actSize;
  3054.         DescType      returnedType;
  3055.         DPtr          theDoc;
  3056.         FSSpec        destFSSpec;
  3057.         
  3058.         myDirObj.dataHandle = nil;
  3059.         
  3060.         /* pick up the direct object, which is the window to save */
  3061.         
  3062.         myErr = AEGetParamDesc(theAppleEvent,
  3063.                                                      keyDirectObject,
  3064.                                                      typeWildCard,
  3065.                                                      &myDirObj);
  3066.                                                      
  3067.         /* pick up optional destination param, if any */
  3068.     
  3069.         tempErr = AEGetParamPtr(theAppleEvent,
  3070.                                                         keyAEDestination,
  3071.                                                       typeFSS,
  3072.                                                         &returnedType,
  3073.                                                         (Ptr)&destFSSpec,
  3074.                                                         sizeof(destFSSpec),
  3075.                                                         &actSize);
  3076.     
  3077.         if (myErr == noErr) 
  3078.             myErr = GotRequiredParams(theAppleEvent);
  3079.             
  3080.         /* get the data */
  3081.     
  3082.         myErr = AECoerceDesc(&myDirObj, typeMyWndw, &newDesc);
  3083.         
  3084.         if (myErr == noErr)
  3085.             if (newDesc.descriptorType!=typeNull) 
  3086.                 {
  3087.                     GetRawDataFromDescriptor(&newDesc,
  3088.                                                                      (Ptr)&theWindowToken,
  3089.                                                                      sizeof(theWindowToken),
  3090.                                                                      &tokenSize);
  3091.                                                                      
  3092.                     ignoreErr = AEDisposeDesc(&newDesc);
  3093.                                                                      
  3094.                     if (theWindowToken) 
  3095.                         {                            
  3096.                             theDoc = DPtrFromWindowPtr(theWindowToken);
  3097.                             
  3098.                             if (theDoc->everSaved == false) 
  3099.                                 if (tempErr != noErr) 
  3100.                                      /* We had no supplied destination and no default either */
  3101.                                     myErr = kAEGenericErr;
  3102.                             
  3103.                             if (myErr==noErr)
  3104.                                 if (tempErr==noErr)    /* we were told where */
  3105.                                     myErr = DoSave(theDoc, destFSSpec); 
  3106.                                 else
  3107.                                     myErr = SaveUsingTemp(theDoc);
  3108.                         }
  3109.                     else
  3110.                         myErr =  errAEIllegalIndex;
  3111.                 }
  3112.                 
  3113.         if (myDirObj.dataHandle) 
  3114.             tempErr = AEDisposeDesc(&myDirObj);
  3115.     
  3116.         return(myErr);
  3117.                     
  3118.     }    /* DoSaveWindow */
  3119.  
  3120. /** -----------------------------------------------------------------------
  3121.         Name:             DoRevertWindow
  3122.         Purpose:        Handles the Revert AppleEvent.
  3123.      -----------------------------------------------------------------------**/
  3124.  
  3125. pascal OSErr DoRevertWindow(const AppleEvent *theAppleEvent,
  3126.                                                      AppleEvent       *reply,
  3127.                                                      long             handlerRefCon)
  3128. #pragma unused (reply, handlerRefCon)
  3129.  
  3130.     OSErr          myErr;
  3131.     OSErr          ignoreErr;
  3132.     AEDesc         myDirObj;
  3133.     AEDesc         newDesc;
  3134.     WindowToken    theWindowToken;
  3135.     Size           tokenSize;
  3136.     DPtr           theDoc;
  3137.     
  3138.     myDirObj.dataHandle = nil;
  3139.     
  3140.   /* pick up the direct object, which is the window to save */
  3141.     
  3142.   myErr = AEGetParamDesc(theAppleEvent,
  3143.                                                  keyDirectObject,
  3144.                                                  typeWildCard,
  3145.                                                  &myDirObj);
  3146.  
  3147.     if (myErr == noErr)
  3148.       myErr = GotRequiredParams(theAppleEvent);
  3149.         
  3150.   /* get the window to revert from the direct object */
  3151.     
  3152.     myErr = AECoerceDesc(&myDirObj, typeMyWndw, &newDesc);
  3153.     
  3154.   if (myErr == noErr) 
  3155.         if (newDesc.descriptorType!=typeNull) 
  3156.             {
  3157.                 GetRawDataFromDescriptor(&newDesc,
  3158.                                                                  (Ptr)&theWindowToken,
  3159.                                                                  sizeof(theWindowToken),
  3160.                                                                  &tokenSize);
  3161.                                                                  
  3162.                 ignoreErr = AEDisposeDesc(&newDesc);
  3163.                                                                  
  3164.                 if (theWindowToken) 
  3165.                     {    
  3166.                         theDoc = DPtrFromWindowPtr(theWindowToken);
  3167.                         
  3168.                         HidePen();
  3169.                         TESetSelect(0, (*(theDoc->theText))->teLength, theDoc->theText);
  3170.                         ShowPen();
  3171.                         TEDelete(theDoc->theText);
  3172.         
  3173.                         if (theDoc->everSaved) 
  3174.                             {
  3175.                                 myErr = GetFileContents(theDoc->theFSSpec, theDoc);
  3176.                                 if (myErr == noErr) 
  3177.                                     {
  3178.                                         ResizeWindow(theDoc);
  3179.                                         theDoc->dirty = false;
  3180.                                     }
  3181.                             }
  3182.         
  3183.                         ShowWindow(theDoc->theWindow); /* <<< Visible already??? */
  3184.                         DoUpdate(theDoc);
  3185.                     }
  3186.                 else
  3187.                     myErr =  errAEIllegalIndex;
  3188.             }
  3189.                 
  3190.     if (myDirObj.dataHandle) 
  3191.         ignoreErr = AEDisposeDesc(&myDirObj);
  3192.         
  3193.   return(myErr);
  3194. }    /* DoRevertWindow */
  3195.  
  3196. /**-----------------------------------------------------------------------
  3197.         Name:             DoPrintDocuments
  3198.         Purpose:        Print a list of documents (or windows).
  3199. -----------------------------------------------------------------------**/
  3200. pascal OSErr DoPrintDocuments(const AppleEvent *message,
  3201.                               AppleEvent       *reply,
  3202.                                                             long refcon)
  3203. {
  3204. #pragma unused (reply, refcon)
  3205.         long          index;
  3206.         long          itemsInList;
  3207.         AEKeyword     keywd;
  3208.         OSErr         err;
  3209.         AEDescList    docList;
  3210.         Size          actSize;
  3211.         DescType      typeCode;
  3212.         FSSpec        theFSSpec;
  3213.         WindowToken   theWindowToken;
  3214.         OSErr         forgetErr;
  3215.         Boolean       talkToUser;
  3216.             
  3217.         err = AEGetParamDesc(message,
  3218.                                                  keyDirectObject,
  3219.                                                  typeAEList,
  3220.                                                  &docList);
  3221.                                                  
  3222.         err = AECountItems(&docList, &itemsInList);
  3223.             
  3224.         for (index = 1; index<=itemsInList; index++)
  3225.             if (err == noErr) 
  3226.                 {
  3227.                     forgetErr = AEGetNthPtr( &docList, index, typeFSS, &keywd,
  3228.                                                                      &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
  3229.                                                                         
  3230.                     if (forgetErr == noErr) 
  3231.                         {
  3232.                             if (err == noErr) 
  3233.                                 err = IssueAEOpenDoc(theFSSpec);
  3234.                                 
  3235.                             if (err == noErr) 
  3236.                                 IssuePrintWindow(FrontWindow());
  3237.                                 
  3238.                             if (err == noErr) 
  3239.                                 IssueCloseCommand(FrontWindow());
  3240.                         }
  3241.                     else
  3242.                         { /* wasn't a file - was it a window ? */
  3243.                             err = AEGetNthPtr(&docList,
  3244.                                               index,
  3245.                                               typeMyWndw,
  3246.                                               &keywd,
  3247.                                                                 &typeCode,
  3248.                                                                 (Ptr)&theWindowToken,
  3249.                                                                 sizeof(WindowToken),
  3250.                                                                 &actSize);
  3251.                                                                     
  3252.                             talkToUser = (AEInteractWithUser(kAEDefaultTimeout, nil, nil) == noErr);
  3253.                             
  3254.                             if (err == noErr) 
  3255.                                 PrintWindow(DPtrFromWindowPtr(theWindowToken), talkToUser);
  3256.                         }
  3257.                 }
  3258.         
  3259.         if (docList.dataHandle)
  3260.             forgetErr = AEDisposeDesc(&docList);
  3261.             
  3262.         return(err);
  3263.     } /* DoPrintDocuments */
  3264.  
  3265.  
  3266.  
  3267. pascal OSErr MyCountProc(    DescType desiredType,
  3268.                                                     DescType containerClass,
  3269.                                                     const AEDesc *container,
  3270.                                                     long *result);
  3271.                                             
  3272. /** -----------------------------------------------------------------------
  3273.         Name:       HandleNumberOfElements
  3274.         Purpose:        Handles the Number Of Elements AppleEvent.
  3275.      -----------------------------------------------------------------------**/
  3276.  
  3277. pascal OSErr HandleNumberOfElements(const AppleEvent *theAppleEvent,
  3278.                                                                     AppleEvent *reply,
  3279.                                                                     long       handlerRefCon)
  3280.   {
  3281. #pragma unused (handlerRefCon)
  3282.  
  3283.       OSErr    myErr;
  3284.       OSErr    forgetErr;
  3285.         AEDesc   myDirObj;
  3286.         DescType myClass;
  3287.         long     myCount;
  3288.         DescType returnedType;
  3289.         Size     actSize;
  3290.         
  3291.         myErr                 = errAEEventNotHandled;
  3292.         myDirObj.dataHandle = nil;
  3293.         
  3294.         /* pick up direct object, which is the container in which things are to be counted */
  3295.         
  3296.         myErr = AEGetParamDesc(theAppleEvent,
  3297.                                                      keyDirectObject,
  3298.                                                      typeWildCard,
  3299.                                                      &myDirObj);
  3300.             
  3301.         /* now the class of objects to be counted */
  3302.         
  3303.         myErr = AEGetParamPtr( theAppleEvent,
  3304.                                                      keyAEObjectClass,
  3305.                                                      typeType,
  3306.                                                      &returnedType,
  3307.                                                      (Ptr)&myClass,
  3308.                                                      sizeof(myClass),
  3309.                                                      &actSize);
  3310.             
  3311.         /* missing any parameters? */
  3312.         
  3313.         myErr = GotRequiredParams(theAppleEvent);
  3314.         
  3315.         /* now count */
  3316.     
  3317.         if (myErr == noErr) 
  3318.             myErr = MyCountProc(myClass,myDirObj.descriptorType, &myDirObj,&myCount);
  3319.         
  3320.         /* add result to reply */
  3321.     
  3322.         if (myErr == noErr) 
  3323.             if (reply->descriptorType != typeNull) 
  3324.                  myErr  = AEPutParamPtr(reply,
  3325.                                                                 keyDirectObject,
  3326.                                                                 typeLongInteger,
  3327.                                                                 (Ptr)&myCount,
  3328.                                                                 sizeof(myCount));
  3329.         if (myErr == noErr)       
  3330.             forgetErr  = AEDisposeDesc(&myDirObj);
  3331.         
  3332.         return(myErr);
  3333.         
  3334.     }    /* HandleNumberOfElements */
  3335.  
  3336. /** -----------------------------------------------------------------------
  3337.         Name:             HandleShowSelection
  3338.         Purpose:        Handles the Make Selection Visible AppleEvent.
  3339.      -----------------------------------------------------------------------**/
  3340.  
  3341. pascal OSErr HandleShowSelection(const AppleEvent *theAppleEvent,
  3342.                                                              AppleEvent       *reply,
  3343.                                                              long             handlerRefCon)
  3344.   {
  3345. #pragma unused (reply,handlerRefCon)
  3346.     
  3347.         OSErr       myErr;
  3348.         OSErr       ignoreErr;
  3349.         AEDesc      myDirObj;
  3350.         AEDesc      newDesc;
  3351.         AEDesc      tokenDesc;
  3352.         Size        actSize;
  3353.         WindowToken theWindowToken;
  3354.         DPtr        theDocument;
  3355.         TEHandle    theHTE;
  3356.         
  3357.         myErr      = errAEEventNotHandled;
  3358.         myDirObj.dataHandle  = nil;
  3359.         tokenDesc.dataHandle = nil;
  3360.         
  3361.         //    pick up direct object, i.e. the window in which to show the selection
  3362.             
  3363.         myErr  = AEGetParamDesc(theAppleEvent,
  3364.                                                         keyDirectObject,
  3365.                                                         typeWildCard,
  3366.                                                         &myDirObj);
  3367.                 
  3368.         //    missing any parameters?
  3369.         
  3370.         myErr = GotRequiredParams(theAppleEvent);
  3371.         
  3372.         //    convert object to WindowToken which we understand
  3373.  
  3374.         myErr = AEResolve(&myDirObj, kAEIDoMinimum, &tokenDesc);
  3375.         
  3376.         if (myErr == noErr)
  3377.             if (tokenDesc.descriptorType==typeMyWndw)
  3378.                 {
  3379.                     if (AECoerceDesc(&myDirObj, typeMyWndw, &newDesc) == noErr) 
  3380.                         {
  3381.                             GetRawDataFromDescriptor(&newDesc,
  3382.                                                                              (Ptr)&theWindowToken,
  3383.                                                                              sizeof(theWindowToken),
  3384.                                                                              &actSize);
  3385.         
  3386.                             ignoreErr = AEDisposeDesc(&newDesc);
  3387.                             
  3388.                             if (myErr==noErr) 
  3389.                                 if (theWindowToken)                                                          
  3390.                                     ShowSelect(DPtrFromWindowPtr(theWindowToken));
  3391.                                 else
  3392.                                     myErr = errAEIllegalIndex;
  3393.                         }
  3394.                 }
  3395.             else
  3396.                 if (tokenDesc.descriptorType==typeMyText)
  3397.                     {
  3398.                         myErr = SetSelectionOfAppleEventObject(keyDirectObject,
  3399.                                                                                           theAppleEvent,
  3400.                                                                                            &theDocument,
  3401.                                                                                           &theHTE);
  3402.                         if (theDocument)
  3403.                           ShowSelect(theDocument);
  3404.                         else
  3405.                             myErr = errAEIllegalIndex;
  3406.                     }
  3407.                 else
  3408.                     myErr = errAEEventNotHandled;
  3409.                     
  3410.         if (myDirObj.dataHandle)
  3411.             ignoreErr = AEDisposeDesc(&myDirObj);
  3412.         
  3413.         if (tokenDesc.dataHandle)
  3414.             ignoreErr = AEDisposeDesc(&tokenDesc);
  3415.             
  3416.         return(myErr);
  3417.         
  3418.     }    /* HandleShowSelection */
  3419.  
  3420. pascal OSErr HandleStartRecording(const AppleEvent *theAppleEvent,
  3421.                                                               AppleEvent *reply,
  3422.                                                               long       handlerRefCon)        
  3423.     {
  3424. #pragma unused (reply,handlerRefCon)
  3425.  
  3426.         OSErr myErr;
  3427.  
  3428.       gBigBrother++;
  3429.     
  3430.         myErr = GotRequiredParams(theAppleEvent);
  3431.  
  3432.         return(myErr);
  3433.         
  3434.     }    /* HandleStartRecording */
  3435.  
  3436. pascal OSErr HandleStopRecording(const AppleEvent *theAppleEvent,
  3437.                                                              AppleEvent *reply,
  3438.                                                                  long handlerRefCon)        
  3439.     {
  3440. #pragma unused (theAppleEvent,reply,handlerRefCon)
  3441.     
  3442.         gBigBrother--;
  3443.         return(noErr);
  3444.     }    /* HandleStopRecording */
  3445.  
  3446.  
  3447. #pragma segment AECommandIssuers
  3448.  
  3449. /*******************************************************************************/
  3450. /* 
  3451.         Start of section involved in building and sending AppleEvent Objects as/with
  3452.         commands
  3453.  */
  3454.  
  3455. /*
  3456.     Make an AEDesc that describes the selection in the window and text edit
  3457.     record supplied
  3458. */
  3459.  
  3460. pascal OSErr MakeWindowObj(WindowPtr theWindow,
  3461.                                                    AEDesc    *dMyDoc)
  3462.     {
  3463.             OSErr    myErr;
  3464.             AEDesc   dNull;
  3465.             AEDesc   dDocIndex;
  3466.             short    winIndex;
  3467.             
  3468.             // AEDesc   dDocName;
  3469.             // Str255   windowName;
  3470.             
  3471.             //  Changed from name to index for better playback
  3472.             //  Needed as new windows now have incremental names
  3473.             //    GetWTitle(theWindow, windowName);
  3474.             //  myErr = AECreateDesc(typeChar,(Ptr)&windowName[1], windowName[0], &dDocName);
  3475.             //  CreateObjSpecifier was formName too…
  3476.             
  3477.             myErr = noErr;
  3478.             
  3479.             winIndex = IndexFromWindowPtr(theWindow);
  3480.             
  3481.             if (winIndex==0)
  3482.                 myErr = errAEIllegalIndex;
  3483.             
  3484.             if (myErr==noErr)
  3485.                 myErr = CreateOffsetDescriptor(winIndex, &dDocIndex);
  3486.             
  3487.             if (myErr==noErr) 
  3488.                 myErr = AECreateDesc(typeNull, nil , 0, &dNull);
  3489.             
  3490.             if (myErr==noErr) 
  3491.                 myErr = CreateObjSpecifier(cWindow, &dNull, formAbsolutePosition, &dDocIndex, true, dMyDoc);
  3492.             
  3493.             return(myErr);
  3494.             
  3495.     } /*MakeWindowObj*/
  3496.     
  3497. pascal OSErr MakeTextObj(WindowPtr theWindow,
  3498.                                                  short     selStart,
  3499.                                                  short     selEnd,
  3500.                                                  AEDesc    *selTextObj)
  3501.     {
  3502.         OSErr    myErr;
  3503.         OSErr    ignoreErr;
  3504.         AEDesc   dMyDoc;
  3505.         AEDesc   startOfs;
  3506.         AEDesc   endOfs;
  3507.         AEDesc   startObj;
  3508.         AEDesc   endObj;
  3509.         AEDesc   rangeDesc;
  3510.         long     startChar;
  3511.         long     endChar;
  3512.         Boolean  spotFlag;
  3513.             
  3514.         myErr = noErr;
  3515.         
  3516.         if (theWindow==nil) 
  3517.             return(noErr);
  3518.             
  3519.         selTextObj->dataHandle = nil;
  3520.         dMyDoc.dataHandle      = nil;
  3521.         startObj.dataHandle    = nil;
  3522.         endObj.dataHandle      = nil;
  3523.         
  3524.         /* 
  3525.             make the window object 
  3526.         */
  3527.         
  3528.         myErr = MakeWindowObj(theWindow, &dMyDoc);
  3529.             
  3530.         if (myErr==noErr)
  3531.             {
  3532.                 /* get the start and end of selection */
  3533.                 
  3534.                 startChar = selStart+1;    /* start counting obj's from 1, not 0 */
  3535.                 endChar   = selEnd;
  3536.                 spotFlag  = (selStart == selEnd);
  3537.                     
  3538.                 myErr = CreateOffsetDescriptor(startChar, &startOfs);
  3539.                             
  3540.                 if (myErr==noErr)
  3541.                     if (spotFlag)
  3542.                         myErr = CreateObjSpecifier(cSpot,
  3543.                                                                              &dMyDoc,
  3544.                                                                              formAbsolutePosition, 
  3545.                                                                              &startOfs, 
  3546.                                                                              true, 
  3547.                                                                              selTextObj);
  3548.                     else
  3549.                         {
  3550.                             /* not a spot - must represent as range */
  3551.                             /* make obj for start char */
  3552.                             
  3553.                             myErr = CreateObjSpecifier(cChar,
  3554.                                                        &dMyDoc,
  3555.                                                                                  formAbsolutePosition,
  3556.                                                                                  &startOfs,
  3557.                                                                                  false,
  3558.                                                                                  &startObj);
  3559.                             
  3560.                             if (myErr==noErr) 
  3561.                                 myErr = CreateOffsetDescriptor(endChar, &endOfs);
  3562.                             
  3563.                             if (myErr==noErr) 
  3564.                                 myErr = CreateObjSpecifier(cChar,
  3565.                                                                                      &dMyDoc,
  3566.                                                                                      formAbsolutePosition,
  3567.                                                                                      &endOfs,
  3568.                                                                                      false,
  3569.                                                                                      &endObj);
  3570.                             
  3571.                             if (myErr==noErr) 
  3572.                                 myErr = CreateRangeDescriptor(&startObj,
  3573.                                                                                             &endObj,
  3574.                                                                                             false,
  3575.                                                                                             &rangeDesc);
  3576.                             
  3577.                             if (myErr==noErr) 
  3578.                                 myErr = CreateObjSpecifier(cChar,
  3579.                                                                                      &dMyDoc,
  3580.                                                                                      formRange,
  3581.                                                                                      &rangeDesc,
  3582.                                                                                      true,
  3583.                                                                                      selTextObj);
  3584.                                                                                      
  3585.                             if (startObj.dataHandle)
  3586.                               ignoreErr = AEDisposeDesc(&startObj);
  3587.                                 
  3588.                             if (startOfs.dataHandle)
  3589.                               ignoreErr = AEDisposeDesc(&startOfs);
  3590.                                 
  3591.                             if (endObj.dataHandle)
  3592.                               ignoreErr = AEDisposeDesc(&endObj);
  3593.                                 
  3594.                             if (endOfs.dataHandle)
  3595.                               ignoreErr = AEDisposeDesc(&endOfs);
  3596.                         }
  3597.             }
  3598.             
  3599.         return(myErr);
  3600.     }
  3601.     
  3602. pascal OSErr MakeSelectedTextObj(WindowPtr theWindow,
  3603.                                                                  TEHandle  theTextEditHandle,
  3604.                                                                  AEDesc    *selTextObj)
  3605.     {
  3606.         return( MakeTextObj(theWindow,
  3607.                                                 (**theTextEditHandle).selStart,
  3608.                                                 (**theTextEditHandle).selEnd,
  3609.                                               selTextObj));
  3610.                                                 
  3611.     }    /* MakeSelectedTextObj */
  3612.  
  3613.  
  3614. /** -----------------------------------------------------------------------
  3615.         Name:             MakeMenuObj
  3616.         Purpose:        Creates a typeObjSpecifier for a named menu.
  3617.      -----------------------------------------------------------------------**/
  3618.  
  3619. pascal OSErr MakeMenuObj(Str255 theName, AEDesc *resultingObj)
  3620.     {
  3621.         AEDesc newDesc;
  3622.         AEDesc dNull;
  3623.         OSErr  theErr;
  3624.         
  3625.         theErr  = AECreateDesc(typeChar,
  3626.                                                      (Ptr)&theName[1],
  3627.                                                      theName[0],
  3628.                                                      &newDesc);
  3629.         if (theErr==noErr)
  3630.             {
  3631.                 theErr = AECreateDesc(typeNull, nil , 0, &dNull);
  3632.                 
  3633.                 theErr = CreateObjSpecifier(cMenu,
  3634.                                                                         &dNull,
  3635.                                                                         formName,
  3636.                                                                         &newDesc,
  3637.                                                                         true,
  3638.                                                                         resultingObj);
  3639.             }
  3640.             
  3641.         return(theErr);
  3642.         
  3643.     } /* MakeMenuObj */
  3644.     
  3645. /** -----------------------------------------------------------------------
  3646.         Name:             MakeMenuItemObj
  3647.         Purpose:        Creates a typeObjSpecifier for a named menu.
  3648.      -----------------------------------------------------------------------**/
  3649.  
  3650. pascal OSErr MakeMenuItemObj(Str255 theName, short theItem, AEDesc *resultingObj)
  3651.     {
  3652.         AEDesc menuDesc;
  3653.         AEDesc dataDesc;
  3654.         
  3655.         OSErr  theErr;
  3656.         
  3657.         theErr  = MakeMenuObj(theName, &menuDesc);
  3658.         
  3659.         if (theErr==noErr)
  3660.             {
  3661.                 theErr = CreateOffsetDescriptor(theItem, &dataDesc);
  3662.                 
  3663.                 theErr = CreateObjSpecifier(cMenu,
  3664.                                                                         &menuDesc,
  3665.                                                                         formAbsolutePosition,
  3666.                                                                         &dataDesc,
  3667.                                                                         true,
  3668.                                                                         resultingObj);
  3669.             }
  3670.             
  3671.         return(theErr);
  3672.     } /* MakeMenuItemObj */
  3673.     
  3674. pascal OSErr MakeClassObj(DescType theClass, AEDesc *myClassObj)
  3675.     {  
  3676.         return(AECreateDesc(typeType,
  3677.                                                      (Ptr)&theClass,
  3678.                                                      sizeof(DescType),
  3679.                                                      myClassObj));
  3680.     } /* MakeClassObj */
  3681.     
  3682. enum editCommandType {
  3683. editCutCommand   = 1,
  3684. editCopyCommand  = 2,
  3685. editPasteCommand = 3,
  3686. editClearCommand = 4
  3687. };
  3688.  
  3689. typedef enum editCommandType editCommandType;
  3690.  
  3691. pascal void DoEditCommand(DPtr theDocument,editCommandType whatCommand);
  3692.  
  3693. pascal void DoEditCommand(DPtr theDocument,editCommandType whatCommand)
  3694.     {
  3695.       OSErr         err;
  3696.       OSErr         forgetErr;
  3697.         AEAddressDesc ourAddress;
  3698.         AppleEvent    editCommandEvent;
  3699.         AppleEvent    ignoreReply;
  3700.         AEDesc        ourTextSelObj;
  3701.         AEEventID     theEventID;
  3702.         AEEventClass  theEventClass;
  3703.  
  3704.         /*
  3705.                 Initialise
  3706.         */
  3707.  
  3708.         ourAddress.dataHandle             = nil;
  3709.         ourTextSelObj.dataHandle         = nil;
  3710.         editCommandEvent.dataHandle = nil;
  3711.         ignoreReply.dataHandle             = nil;
  3712.         
  3713.         err = MakeSelfAddress(&ourAddress);
  3714.         
  3715.         /*
  3716.             Build an object to represent the current document's selection
  3717.         */
  3718.         err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &ourTextSelObj);
  3719.         
  3720.         if (err==noErr)
  3721.           {
  3722.                 switch (whatCommand) {
  3723.                     case  editCutCommand         : theEventID    = kAECut;
  3724.                                                                         theEventClass = kAEMiscStandards;
  3725.                                                                         break;
  3726.                     case  editCopyCommand     : theEventID    = kAECopy;
  3727.                                                                         theEventClass = kAEMiscStandards;
  3728.                                                                         break;
  3729.                     case  editPasteCommand     : theEventID    = kAEPaste;
  3730.                                                                         theEventClass = kAEMiscStandards;
  3731.                                                                         break;
  3732.                     case  editClearCommand     : theEventID    = kAEDelete;
  3733.                                                                         theEventClass = kAECoreSuite;
  3734.                                                                         break;
  3735.                 }
  3736.                     
  3737.                 err = AECreateAppleEvent( theEventClass, theEventID, &ourAddress, 0, 0, &editCommandEvent);    
  3738.  
  3739.                 /* add parameter */    
  3740.                 if (err==noErr) 
  3741.                     err = AEPutParamDesc(&editCommandEvent, keyDirectObject, &ourTextSelObj);
  3742.                     
  3743.                 /*and now Send the message*/
  3744.                 if (err==noErr)
  3745.                     err = AESend(&editCommandEvent,&ignoreReply,kAENoReply,kAENormalPriority,10000,nil, nil);
  3746.             }              
  3747.             
  3748.         /*
  3749.             Clean up
  3750.         */
  3751.         if (ourAddress.dataHandle) 
  3752.             forgetErr = AEDisposeDesc(&ourAddress);
  3753.         
  3754.         if (editCommandEvent.dataHandle) 
  3755.             forgetErr = AEDisposeDesc(&editCommandEvent);
  3756.         
  3757.         if (ignoreReply.dataHandle) 
  3758.             forgetErr = AEDisposeDesc(&ignoreReply);
  3759.             
  3760.         if (ourTextSelObj.dataHandle) 
  3761.             forgetErr = AEDisposeDesc(&ourTextSelObj);
  3762.             
  3763.     } /*DoEditCommand*/
  3764.     
  3765. pascal void IssueCutCommand(DPtr theDocument)
  3766.     {            
  3767.         DoEditCommand(theDocument, editCutCommand);
  3768.     } 
  3769.     
  3770. pascal void IssueCopyCommand(DPtr theDocument)
  3771.     {
  3772.         DoEditCommand(theDocument, editCopyCommand);
  3773.     }
  3774.     
  3775. pascal void IssuePasteCommand(DPtr theDocument)
  3776.     {
  3777.         DoEditCommand(theDocument, editPasteCommand);    
  3778.     }
  3779.     
  3780. pascal void IssueClearCommand(DPtr theDocument)
  3781.     {
  3782.         DoEditCommand(theDocument, editClearCommand);    
  3783.     }
  3784.     
  3785. pascal void IssueFontCommand(DPtr theDocument, short theItem)
  3786.     {
  3787.        Str255         name;
  3788.          AEDesc           strDesc;
  3789.          AEAddressDesc  theAddress;
  3790.          OSErr            err;
  3791.          AEDesc         selTextObj;
  3792.             
  3793.         err = MakeSelfAddress(&theAddress);
  3794.         
  3795.         err = MakeSelectedTextObj(theDocument->theWindow,
  3796.                                                             theDocument->theText,
  3797.                                                             &selTextObj);
  3798.         
  3799.         GetItem(myMenus[fontM], theItem, name);
  3800.         
  3801.         if (err==noErr)
  3802.             err  = AECreateDesc(typeChar,
  3803.                                                     (Ptr)&name[1],
  3804.                                                     name[0],
  3805.                                                     &strDesc);
  3806.         
  3807.         if (err==noErr)
  3808.             err  = SendAESetObjProp(&selTextObj, 
  3809.                                                             pFont,
  3810.                                                             &strDesc,
  3811.                                                             &theAddress);                                                            
  3812.     }
  3813.     
  3814. /*
  3815.     Window property routines
  3816. */
  3817.  
  3818. pascal void IssueZoomCommand(WindowPtr whichWindow, short whichPart)
  3819.     {
  3820.       Boolean       zoomBool;
  3821.         AEDesc        zoomDesc;
  3822.         AEAddressDesc selfAddr;
  3823.         AEDesc        frontWinObj;
  3824.         OSErr         err;
  3825.     
  3826.         err = MakeSelfAddress(&selfAddr);
  3827.         
  3828.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3829.         
  3830.         zoomBool = (whichPart==inZoomOut);
  3831.     
  3832.         err = AECreateDesc(typeBoolean,
  3833.                                              (Ptr)&zoomBool,
  3834.                                              sizeof(zoomBool),
  3835.                                              &zoomDesc);
  3836.                                                 
  3837.         err = SendAESetObjProp(&frontWinObj, 
  3838.                                                      pIsZoomed,
  3839.                                                      &zoomDesc,
  3840.                                                      &selfAddr);                                                            
  3841.     } /* IssueZoomCommand */
  3842.     
  3843. pascal void IssueCloseCommand(WindowPtr whichWindow)
  3844.     {
  3845.         AEAddressDesc  selfAddr;
  3846.         AEDesc         frontWinObj;
  3847.         OSErr          err;
  3848.         OSErr          ignoreErr;
  3849.         AppleEvent     closeCommandEvent;
  3850.         AppleEvent     ignoreReply;
  3851.     
  3852.         frontWinObj.dataHandle = nil;
  3853.         
  3854.         err = MakeSelfAddress(&selfAddr);
  3855.         
  3856.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3857.                                                             
  3858.         err = AECreateAppleEvent( kAECoreSuite, kAEClose, &selfAddr, 0, 0, &closeCommandEvent) ;                
  3859.         
  3860.         /* add parameter - the window to close */    
  3861.         if (err==noErr) 
  3862.             err = AEPutParamDesc(&closeCommandEvent, keyDirectObject, &frontWinObj);
  3863.             
  3864.         if (err==noErr) 
  3865.             err = AESend(&closeCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAENormalPriority,10000,nil, nil);
  3866.         
  3867.         if (closeCommandEvent.dataHandle) 
  3868.             ignoreErr = AEDisposeDesc(&closeCommandEvent);
  3869.         
  3870.         if (frontWinObj.dataHandle) 
  3871.             ignoreErr = AEDisposeDesc(&frontWinObj);
  3872.             
  3873.         if (selfAddr.dataHandle) 
  3874.             ignoreErr = AEDisposeDesc(&selfAddr);
  3875.             
  3876.     } /* IssueCloseCommand */
  3877.     
  3878. pascal void IssueSizeWindow(WindowPtr whichWindow, short newHSize, short newVSize)
  3879.     {
  3880.       Rect          sizeRect;
  3881.         Rect          contentRect;
  3882.         short         edgeSize;
  3883.         AEDesc        sizeDesc;
  3884.         AEAddressDesc selfAddr;
  3885.         AEDesc        frontWinObj;
  3886.         OSErr         err;
  3887.     
  3888.         sizeRect    = (**(((WindowPeek)whichWindow)->strucRgn)).rgnBBox;
  3889.         contentRect = (**(((WindowPeek)whichWindow)->contRgn)).rgnBBox;
  3890.         
  3891.         edgeSize = sizeRect.right-sizeRect.left-(contentRect.right-contentRect.left);
  3892.         sizeRect.right = sizeRect.left+newHSize+edgeSize;
  3893.         
  3894.         edgeSize = sizeRect.bottom-sizeRect.top-(contentRect.bottom-contentRect.top);
  3895.         sizeRect.bottom = sizeRect.top+newVSize+edgeSize;
  3896.         
  3897.         err = MakeSelfAddress(&selfAddr);
  3898.         
  3899.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3900.         
  3901.         if (err==noErr)
  3902.             err = AECreateDesc(typeQDRectangle,
  3903.                                                  (Ptr)&sizeRect,
  3904.                                                  sizeof(sizeRect),
  3905.                                                  &sizeDesc);
  3906.         
  3907.         if (err==noErr)
  3908.             err  = SendAESetObjProp(&frontWinObj, 
  3909.                                                             pBounds,
  3910.                                                             &sizeDesc,
  3911.                                                             &selfAddr);                                                            
  3912.     } /*IssueSizeWindow*/
  3913.     
  3914. pascal void IssueMoveWindow(WindowPtr whichWindow, Rect sizeRect)
  3915.     {
  3916.         AEDesc        sizeDesc;
  3917.         AEAddressDesc selfAddr;
  3918.         AEDesc        frontWinObj;
  3919.         OSErr         err;
  3920.     
  3921.         err = MakeSelfAddress(&selfAddr);
  3922.         
  3923.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3924.             
  3925.         if (err==noErr)
  3926.             err = AECreateDesc(typeQDRectangle,
  3927.                                                  (Ptr)&sizeRect,
  3928.                                                  sizeof(sizeRect),
  3929.                                                  &sizeDesc);
  3930.         
  3931.         if (err==noErr)        
  3932.             err = SendAESetObjProp(&frontWinObj, 
  3933.                                                          pBounds,
  3934.                                                          &sizeDesc,
  3935.                                                          &selfAddr);                                                            
  3936.     } /*IssueMoveWindow*/
  3937.  
  3938. pascal void IssuePageSetupWindow(WindowPtr whichWindow, TPrint thePageSetup)
  3939.     {
  3940.         AEDesc        sizeDesc;
  3941.         AEAddressDesc selfAddr;
  3942.         AEDesc        frontWinObj;
  3943.         OSErr         err;
  3944.     
  3945.         err = MakeSelfAddress(&selfAddr);
  3946.         
  3947.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3948.         
  3949.         if (err==noErr)
  3950.             err = AECreateDesc(typeTPrint,
  3951.                                                  (Ptr)&thePageSetup,
  3952.                                                  sizeof(thePageSetup),
  3953.                                                  &sizeDesc);
  3954.                                                  
  3955.         if (err==noErr)
  3956.             err = SendAESetObjProp(&frontWinObj, 
  3957.                                                          pPageSetup,
  3958.                                                          &sizeDesc,
  3959.                                                          &selfAddr);                                                            
  3960.                                                      
  3961.     } /*IssuePageSetupWindow*/
  3962.  
  3963.     
  3964. pascal void IssuePrintWindow(WindowPtr whichWindow)
  3965.     {
  3966.         AEAddressDesc selfAddr;
  3967.         AEDesc        frontWinObj;
  3968.         OSErr         err;
  3969.         OSErr         ignoreErr;
  3970.         AppleEvent    printCommandEvent;
  3971.         AppleEvent    ignoreReply;
  3972.     
  3973.         err = MakeSelfAddress(&selfAddr);
  3974.         
  3975.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3976.                                                             
  3977.         err = AECreateAppleEvent(kCoreEventClass, kAEPrintDocuments, &selfAddr, 0, 0, &printCommandEvent) ;                
  3978.  
  3979.         /* 
  3980.             add parameter - the window to print
  3981.         */    
  3982.  
  3983.         if (err==noErr) 
  3984.             err = AEPutParamDesc(&printCommandEvent, keyDirectObject, &frontWinObj);
  3985.             
  3986.         if (err==noErr) 
  3987.             err = AESend(&printCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAENormalPriority,10000,nil, nil);
  3988.     
  3989.       if (printCommandEvent.dataHandle)
  3990.             ignoreErr = AEDisposeDesc(&printCommandEvent);
  3991.         
  3992.         if (frontWinObj.dataHandle) 
  3993.             ignoreErr = AEDisposeDesc(&frontWinObj);
  3994.             
  3995.         if (selfAddr.dataHandle) 
  3996.             ignoreErr = AEDisposeDesc(&selfAddr);
  3997.             
  3998.     } /*IssuePrintWindow*/
  3999.     
  4000. pascal OSErr IssueAEOpenDoc(FSSpec myFSSpec)
  4001. /* send OpenDocs AppleEvent to myself, with a one-element list
  4002.   containing the given file spec
  4003.  
  4004.   NOTES : the core AEOpenDocs event is defined as taking a list of
  4005.           aliases (not file specs) as its direct parameter.  However,
  4006.             we can send the file spec instead and depend on AppleEvents'
  4007.             automatic coercion.  In fact, we don't really even have to put 
  4008.             in a list; AppleEvents will coerce a descriptor into a 1-element
  4009.             list if called for.  In this routine, though, we'll make the
  4010.             list for demonstration purposes.
  4011. */
  4012.  
  4013.  {
  4014.          AppleEvent    myAppleEvent;
  4015.         AppleEvent    defReply;
  4016.         AEDescList    docList;
  4017.         AEAddressDesc selfAddr;
  4018.         OSErr         myErr;
  4019.         OSErr         ignoreErr;
  4020.         
  4021.         myAppleEvent.dataHandle = nil;
  4022.         docList.dataHandle  = nil;
  4023.         selfAddr.dataHandle = nil;
  4024.         defReply.dataHandle = nil;
  4025.             
  4026.         /*
  4027.             Create empty list and add one file spec
  4028.         */
  4029.         myErr = AECreateList(nil,0,false, &docList);
  4030.         
  4031.         if (myErr==noErr) 
  4032.             myErr = AEPutPtr(&docList,1,typeFSS,(Ptr)&myFSSpec,sizeof(myFSSpec));
  4033.             
  4034.         /*
  4035.             Create a self address to send it to
  4036.         */
  4037.         if (myErr==noErr) 
  4038.             myErr = MakeSelfAddress(&selfAddr);
  4039.             
  4040.         if (myErr==noErr) 
  4041.             myErr = AECreateAppleEvent(kCoreEventClass,
  4042.                                                                  kAEOpenDocuments,
  4043.                                                                  &selfAddr,
  4044.                                                                  kAutoGenerateReturnID,
  4045.                                                                  kAnyTransactionID,
  4046.                                                                  &myAppleEvent);
  4047.     
  4048.         /* 
  4049.             Put Params into our event and send it
  4050.         */
  4051.         if (myErr == noErr) 
  4052.             myErr = AEPutParamDesc(&myAppleEvent,
  4053.                                                          keyDirectObject,
  4054.                                                          &docList);
  4055.     
  4056.         myErr = AESend(&myAppleEvent,
  4057.                                      &defReply,
  4058.                                      kAENoReply+kAEAlwaysInteract,
  4059.                                      kAENormalPriority,
  4060.                                      kAEDefaultTimeout,
  4061.                                      nil,
  4062.                                      nil);
  4063.             
  4064.         if (selfAddr.dataHandle) 
  4065.             ignoreErr = AEDisposeDesc(&selfAddr);
  4066.             
  4067.         if (myAppleEvent.dataHandle) 
  4068.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4069.             
  4070.         if (docList.dataHandle) 
  4071.             ignoreErr = AEDisposeDesc(&docList);
  4072.             
  4073.         return(myErr);
  4074.         
  4075.     }    /* IssueAEOpenDoc */
  4076.  
  4077. pascal void IssueAENewWindow(void)
  4078. /* 
  4079.     send the New Element event to myself with a null container
  4080. */
  4081.     {
  4082.       AppleEvent    myAppleEvent;
  4083.         AppleEvent    defReply;
  4084.         AEAddressDesc selfAddr;
  4085.         OSErr         myErr;
  4086.         OSErr         ignoreErr;
  4087.         DescType      elemClass;
  4088.         
  4089.         myAppleEvent.dataHandle = nil;
  4090.         
  4091.         /*
  4092.             Create the address of us
  4093.         */
  4094.         
  4095.         myErr = MakeSelfAddress(&selfAddr);
  4096.         
  4097.         /*
  4098.             create event 
  4099.         */
  4100.         
  4101.         myErr = AECreateAppleEvent(kAECoreSuite,
  4102.                                                              kAECreateElement,
  4103.                                                              &selfAddr,
  4104.                                                              kAutoGenerateReturnID,
  4105.                                                              kAnyTransactionID,
  4106.                                                              &myAppleEvent);
  4107.         
  4108.         /*
  4109.             attach desired class of new element
  4110.         */
  4111.         
  4112.         elemClass = cWindow;
  4113.         
  4114.         if (myErr == noErr) 
  4115.             myErr = AEPutParamPtr(&myAppleEvent,
  4116.                                                       keyAEObjectClass,
  4117.                                                         typeType,
  4118.                                                         (Ptr)&elemClass,
  4119.                                                         sizeof(elemClass));
  4120.             
  4121.         /*
  4122.             send the event 
  4123.         */
  4124.         
  4125.         if (myErr == noErr) 
  4126.             myErr = AESend(&myAppleEvent,
  4127.                                          &defReply,
  4128.                                          kAENoReply+kAENeverInteract,
  4129.                                          kAENormalPriority,
  4130.                                          kAEDefaultTimeout,
  4131.                                          nil,
  4132.                                          nil);
  4133.         /*
  4134.             Clean up - reply never created so don't throw away
  4135.         */
  4136.         if (selfAddr.dataHandle) 
  4137.             ignoreErr = AEDisposeDesc(&selfAddr);
  4138.             
  4139.         if (myAppleEvent.dataHandle) 
  4140.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4141.                     
  4142.     }    /* IssueAENewWindow */
  4143.  
  4144. pascal OSErr IssueSaveCommand(WindowPtr theWindow,
  4145.                               FSSpecPtr where)
  4146. /*
  4147.     send an AppleEvent Save Event to myself
  4148. */
  4149.  
  4150. {
  4151.     AEDesc        windowObj;
  4152.     AppleEvent    myAppleEvent;
  4153.     AppleEvent    defReply;
  4154.     OSErr         myErr;
  4155.     OSErr         ignoreErr;
  4156.     AEAddressDesc selfAddr;
  4157.         
  4158.     windowObj.dataHandle = nil;
  4159.     myAppleEvent.dataHandle = nil;
  4160.     
  4161.     myErr = MakeWindowObj(theWindow, &windowObj);
  4162.             
  4163.     if (myErr==noErr) 
  4164.         myErr = MakeSelfAddress(&selfAddr);
  4165.         
  4166.   /*
  4167.         Build event
  4168.     */
  4169.     
  4170.   if (myErr == noErr) 
  4171.         myErr = AECreateAppleEvent(kAECoreSuite,
  4172.                                                              kAESave,
  4173.                                                              &selfAddr,
  4174.                                                              kAutoGenerateReturnID,
  4175.                                                              kAnyTransactionID,
  4176.                                                              &myAppleEvent);
  4177.       
  4178.   /*
  4179.         say which window
  4180.     */
  4181.     
  4182.   if (myErr==noErr) 
  4183.         myErr = AEPutParamDesc(&myAppleEvent,
  4184.                                                      keyDirectObject,
  4185.                                                      &windowObj);
  4186.   
  4187.   /*
  4188.         add optional file param if we need to
  4189.     */
  4190.     
  4191.   if (where) 
  4192.         if (myErr==noErr) 
  4193.             myErr = AEPutParamPtr(&myAppleEvent,
  4194.                                                         keyAEDestination,
  4195.                                                         typeFSS,
  4196.                                                         (Ptr)where,
  4197.                                                         sizeof(FSSpec));
  4198.         
  4199.   /*
  4200.         send the event
  4201.     */
  4202.   if (myErr==noErr) 
  4203.         myErr  = AESend(&myAppleEvent,
  4204.                                         &defReply,
  4205.                                         kAENoReply+kAENeverInteract,
  4206.                                         kAENormalPriority,
  4207.                                         kAEDefaultTimeout,
  4208.                                         nil,
  4209.                                         nil);
  4210.       
  4211.     if (selfAddr.dataHandle) 
  4212.         ignoreErr = AEDisposeDesc(&selfAddr);
  4213.       
  4214.     if (windowObj.dataHandle) 
  4215.         ignoreErr = AEDisposeDesc(&windowObj);
  4216.         
  4217.     if (myAppleEvent.dataHandle) 
  4218.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  4219.         
  4220.     return(myErr);
  4221. }    /* IssueSaveCommand */
  4222.  
  4223. pascal void IssueSelectWindowCommand(WindowPtr theWindow, WindowPtr oldFrontWindow)
  4224. /*
  4225.     send an AppleEvent Move Event to ourself to select the chosen window
  4226.     i.e. bring it to the front. This is the same as move before the front window.
  4227. */
  4228.  
  4229. {
  4230.     AEDesc        windowObj;
  4231.     AEDesc        fWinObj;
  4232.     AEDesc        theDestObject;
  4233.     AEDesc        theDestRecord;
  4234.     DescType      thePosn;
  4235.     AppleEvent    myAppleEvent;
  4236.     AppleEvent    defReply;
  4237.     OSErr         myErr;
  4238.     OSErr         ignoreErr;
  4239.     AEAddressDesc selfAddr;
  4240.         
  4241.     windowObj.dataHandle    = nil;
  4242.     myAppleEvent.dataHandle = nil;
  4243.     
  4244.     myErr = MakeWindowObj(theWindow, &windowObj);
  4245.             
  4246.     if (myErr==noErr) 
  4247.         myErr = MakeSelfAddress(&selfAddr);
  4248.         
  4249.   /*
  4250.         Build event
  4251.     */
  4252.     
  4253.   if (myErr == noErr) 
  4254.         myErr = AECreateAppleEvent(kAECoreSuite,
  4255.                                                              kAEMove,
  4256.                                                              &selfAddr,
  4257.                                                              kAutoGenerateReturnID,
  4258.                                                              kAnyTransactionID,
  4259.                                                              &myAppleEvent);
  4260.       
  4261.   /*
  4262.         say which window
  4263.     */
  4264.     
  4265.   if (myErr==noErr) 
  4266.         myErr = AEPutParamDesc(&myAppleEvent,
  4267.                                                      keyDirectObject,
  4268.                                                      &windowObj);
  4269.   
  4270.   /*
  4271.         add to where param - typeInsertionLoc
  4272.     */
  4273.     
  4274.     if (myErr==noErr)
  4275.         {
  4276.             // Build a record
  4277.             
  4278.             theDestRecord.dataHandle = nil;
  4279.             theDestObject.dataHandle = nil;
  4280.             fWinObj.dataHandle       = nil;
  4281.             
  4282.             myErr = AECreateList(nil, 0, true, &theDestRecord);
  4283.             
  4284.             if (myErr==noErr)
  4285.                 { // add the reference object - the current front window
  4286.  
  4287.                     myErr = MakeWindowObj(oldFrontWindow, &fWinObj);
  4288.                     
  4289.                     if (myErr==noErr)
  4290.                         myErr = AEPutKeyDesc(&theDestRecord,
  4291.                                                                  keyAEObject,
  4292.                                                                  &fWinObj);
  4293.                     
  4294.                     if (fWinObj.dataHandle)
  4295.                         ignoreErr = AEDisposeDesc(&fWinObj);
  4296.                 }
  4297.             
  4298.             // How to place it relative to the reference object
  4299.             thePosn = kAEBefore;
  4300.             
  4301.             if (myErr==noErr)
  4302.                 myErr = AEPutKeyPtr(&theDestRecord,
  4303.                                                       keyAEPosition,
  4304.                                                       typeEnumeration,
  4305.                                                       (Ptr)&thePosn,
  4306.                                                       sizeof(thePosn));
  4307.             
  4308.             if (myErr==noErr)
  4309.                 myErr = AECoerceDesc(&theDestRecord, typeInsertionLoc, &theDestObject);
  4310.             
  4311.             if (theDestRecord.dataHandle)
  4312.                 ignoreErr = AEDisposeDesc(&theDestRecord);
  4313.             
  4314.             if (myErr==noErr)
  4315.                 myErr = AEPutParamDesc(&myAppleEvent,
  4316.                                                              keyAEInsertHere,
  4317.                                                              &theDestObject);
  4318.                                                          
  4319.             if (theDestObject.dataHandle)
  4320.                 ignoreErr = AEDisposeDesc(&theDestObject);
  4321.         }
  4322.         
  4323.   /*
  4324.         send the event
  4325.     */
  4326.   if (myErr==noErr) 
  4327.         myErr  = AESend(&myAppleEvent,
  4328.                                         &defReply,
  4329.                                         kAENoReply+kAEDontExecute,
  4330.                                         kAENormalPriority,
  4331.                                         kAEDefaultTimeout,
  4332.                                         nil,
  4333.                                         nil);
  4334.       
  4335.     if (selfAddr.dataHandle) 
  4336.         ignoreErr = AEDisposeDesc(&selfAddr);
  4337.       
  4338.     if (windowObj.dataHandle) 
  4339.         ignoreErr = AEDisposeDesc(&windowObj);
  4340.         
  4341.     if (myAppleEvent.dataHandle) 
  4342.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  4343.         
  4344. }    /* IssueSelectWindowCommand */
  4345.  
  4346. pascal OSErr IssueRevertCommand(WindowPtr theWindow)
  4347. /*
  4348.     send an AppleEvent Revert Event to myself
  4349. */
  4350.  
  4351.     {
  4352.         AEDesc        windowObj;
  4353.         AppleEvent    myAppleEvent;
  4354.         AppleEvent    defReply;
  4355.         OSErr         myErr;
  4356.         OSErr         ignoreErr;
  4357.         AEAddressDesc selfAddr;
  4358.         
  4359.         windowObj.dataHandle = nil;
  4360.         myAppleEvent.dataHandle = nil;
  4361.         
  4362.         myErr = MakeWindowObj(theWindow, &windowObj);
  4363.                     
  4364.         if (myErr==noErr) 
  4365.             myErr = MakeSelfAddress(&selfAddr);
  4366.             
  4367.         /*
  4368.             Build event
  4369.         */
  4370.         
  4371.         if (myErr == noErr) 
  4372.             myErr  = AECreateAppleEvent(kAEMiscStandards,
  4373.                                                                     kAERevert,
  4374.                                                                     &selfAddr,
  4375.                                                                     kAutoGenerateReturnID,
  4376.                                                                     kAnyTransactionID,
  4377.                                                                     &myAppleEvent);
  4378.         /*
  4379.             say which window
  4380.         */
  4381.         
  4382.         if (myErr == noErr) 
  4383.             myErr = AEPutParamDesc(&myAppleEvent,
  4384.                                                          keyDirectObject,
  4385.                                                          &windowObj);
  4386.         /*
  4387.             send the event
  4388.         */
  4389.         if (myErr==noErr) 
  4390.             myErr  = AESend(&myAppleEvent,
  4391.                                             &defReply,
  4392.                                             kAENoReply+kAENeverInteract,
  4393.                                             kAENormalPriority,
  4394.                                             kAEDefaultTimeout,
  4395.                                             nil,
  4396.                                             nil);
  4397.             
  4398.         if (windowObj.dataHandle) 
  4399.             ignoreErr = AEDisposeDesc(&windowObj);
  4400.             
  4401.         if (myAppleEvent.dataHandle) 
  4402.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4403.             
  4404.         if (selfAddr.dataHandle) 
  4405.             ignoreErr = AEDisposeDesc(&selfAddr);
  4406.             
  4407.         return(myErr);
  4408.     }    /* IssueRevertCommand */
  4409.  
  4410. /*
  4411.     Name : IssueQuitCommand
  4412.     Purpose : Sends self a Quit AppleEvent
  4413. */
  4414. pascal OSErr IssueQuitCommand(void)
  4415.     {
  4416.         AppleEvent    myAppleEvent;
  4417.         AppleEvent    defReply;
  4418.         OSErr         myErr;
  4419.         OSErr         ignoreErr;
  4420.         AEAddressDesc selfAddr;
  4421.         DescType      mySaveOpt;
  4422.         
  4423.         myAppleEvent.dataHandle = nil;
  4424.         selfAddr.dataHandle     = nil;
  4425.                             
  4426.         myErr = MakeSelfAddress(&selfAddr);
  4427.             
  4428.         /*
  4429.             Build event
  4430.         */
  4431.         
  4432.         if (myErr == noErr) 
  4433.             myErr  = AECreateAppleEvent(kCoreEventClass,
  4434.                                                                     kAEQuitApplication,
  4435.                                                                     &selfAddr,
  4436.                                                                     kAutoGenerateReturnID,
  4437.                                                                     kAnyTransactionID,
  4438.                                                                     &myAppleEvent);
  4439.         /*
  4440.             say which save option
  4441.         */
  4442.         
  4443.         mySaveOpt = kAEAsk;
  4444.         
  4445.         if (myErr == noErr) 
  4446.             myErr = AEPutParamPtr(&myAppleEvent,
  4447.                                                         keyAESaveOptions,
  4448.                                                         typeEnumerated,
  4449.                                                         (Ptr)&mySaveOpt,
  4450.                                                         sizeof(mySaveOpt));
  4451.         /*
  4452.             send the event
  4453.         */
  4454.         if (myErr==noErr) 
  4455.             myErr  = AESend(&myAppleEvent,
  4456.                                             &defReply,
  4457.                                             kAENoReply+kAEAlwaysInteract,
  4458.                                             kAENormalPriority,
  4459.                                             kAEDefaultTimeout,
  4460.                                             nil,
  4461.                                             nil);
  4462.                         
  4463.         if (myAppleEvent.dataHandle) 
  4464.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4465.             
  4466.         if (selfAddr.dataHandle) 
  4467.             ignoreErr = AEDisposeDesc(&selfAddr);
  4468.             
  4469.         return(myErr);
  4470.     }    /* IssueQuitCommand */
  4471.  
  4472.     
  4473. #define kOK 1
  4474. #define kCancel 2
  4475. #define kOtherSize 4
  4476. #define kOutlineItem 5
  4477.  
  4478. pascal Boolean PoseSizeDialog(long *whatSize);
  4479.  
  4480. pascal Boolean PoseSizeDialog(long *whatSize)
  4481.   {
  4482.       GrafPtr   savedPort;
  4483.     DialogPtr aDialog;
  4484.       Str255    aString;
  4485.       short     itemHit;
  4486.  
  4487.         GetPort(&savedPort);
  4488.         aDialog = GetNewDialog(1004, nil, (WindowPtr)-1);
  4489.         ShowWindow(aDialog);
  4490.         SetPort(aDialog);
  4491.         
  4492.         AdornDefaultButton(aDialog, kOutlineItem);
  4493.          
  4494.         /*set the edittext button to contain the right size*/
  4495.         NumToString(*whatSize, aString);
  4496.         SetText(aDialog, kOtherSize, aString);
  4497.      
  4498.         do {
  4499.             ModalDialog(nil, &itemHit);
  4500.         } while ((itemHit!=kOK) && (itemHit!=kCancel));
  4501.         
  4502.         if (itemHit == kOK) 
  4503.             RetrieveText(aDialog, kOtherSize, aString);
  4504.     
  4505.         DisposDialog(aDialog);
  4506.         SetPort(savedPort);
  4507.         
  4508.         if (itemHit == kOK) 
  4509.          {
  4510.              /*set the new size of the text*/
  4511.              StringToNum(aString, whatSize);
  4512.              if ((*whatSize<1) || (*whatSize>2000)) 
  4513.                  *whatSize = 12;
  4514.          }
  4515.         return(itemHit == kOK);
  4516.     }
  4517.     
  4518. pascal void IssueSizeCommand(DPtr theDocument,short theItem)
  4519.     {
  4520.         Str255        name;
  4521.         AEDesc        sizeDesc;
  4522.         AEAddressDesc theAddress;
  4523.         OSErr         err;
  4524.         AEDesc        selTextObj;
  4525.         
  4526.         /*
  4527.             Vars to do with menu processing
  4528.         */
  4529.         short     lastSize;
  4530.         short     upItem;
  4531.         short     downItem;
  4532.         short     otherItem;
  4533.         long      theSize;
  4534.         TextStyle theStyle;
  4535.         short     lineHeight;
  4536.         short     fontAscent;
  4537.             
  4538.         err = MakeSelfAddress(&theAddress);
  4539.         
  4540.         err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);        
  4541.  
  4542.      /*check if the item is on the Size menu*/
  4543.      /*remembering that we can add and delete items from it*/
  4544.      lastSize  = CountMItems(myMenus[sizeM]) - 5;
  4545.      upItem    = lastSize + 2;
  4546.      downItem  = upItem + 1;
  4547.      otherItem = downItem + 2;
  4548.       
  4549.      TEGetStyle((**(theDocument->theText)).selStart,
  4550.                 &theStyle,
  4551.                             &lineHeight,
  4552.                             &fontAscent,
  4553.                             theDocument->theText);
  4554.         
  4555.      GetItem(myMenus[sizeM], theItem, name);
  4556.      
  4557.      if (theItem <= lastSize)
  4558.        {
  4559.          GetItem(myMenus[sizeM], theItem, name);
  4560.        StringToNum(name, &theSize);
  4561.        }
  4562.      else
  4563.        if (theItem == upItem)  
  4564.             theSize = theStyle.tsSize+1;
  4565.          else
  4566.             if (theItem == downItem)
  4567.                       theSize = theStyle.tsSize-1;
  4568.                  else 
  4569.                     if (theItem == otherItem) 
  4570.                         {
  4571.                           theSize = theStyle.tsSize;
  4572.                             if (!PoseSizeDialog(&theSize))
  4573.                                 return;
  4574.                         }
  4575.  
  4576.         if (err==noErr)
  4577.           err = CreateOffsetDescriptor(theSize, &sizeDesc);
  4578.             
  4579.         if (err==noErr)
  4580.             err = SendAESetObjProp(&selTextObj, 
  4581.                                                          pPointSize,
  4582.                                                          &sizeDesc,
  4583.                                                          &theAddress);
  4584.                                                      
  4585.     } /*IssueSizeCommand*/
  4586.     
  4587. pascal void IssueStyleCommand(DPtr theDocument, short theItem)
  4588.     {
  4589.         Style         theFace;
  4590.         OSErr         err;
  4591.         AEDesc        result;
  4592.         AEAddressDesc selfAddr;
  4593.         AEDesc        selTextObj;
  4594.         TextStyle     theStyle;
  4595.         short         lineHeight;
  4596.         short         fontAscent;
  4597.             
  4598.         TEGetStyle((**(theDocument->theText)).selStart,
  4599.                    &theStyle,
  4600.                              &lineHeight,
  4601.                              &fontAscent,
  4602.                              theDocument->theText);
  4603.         
  4604.         theFace = 0;
  4605.     switch (theItem) {
  4606.             case cPlain              : theFace = 0;
  4607.                                 break;
  4608.             case cBold                : theFace = bold;
  4609.                                 break;
  4610.             case cItalic            : theFace = italic;
  4611.                                 break;
  4612.             case cUnderline      : theFace = underline;
  4613.                                 break;
  4614.             case cOutline          : theFace = outline;
  4615.                                 break;
  4616.             case cShadow            : theFace = shadow;
  4617.                                 break;
  4618.             case cCondense        : theFace = condense;
  4619.                                 break;
  4620.             case cExtend            : theFace = extend;
  4621.                                 break;
  4622.     } /*of case */
  4623.  
  4624.     if (theFace==0)
  4625.             err = BuildTypeTextStylesDesc(0, bold+italic+underline+outline+shadow+condense+extend, &result);
  4626.     else
  4627.       if (theFace & theStyle.tsFace)
  4628.               err = BuildTypeTextStylesDesc(0, theFace, &result);
  4629.             else
  4630.               err = BuildTypeTextStylesDesc(theFace, 0, &result);
  4631.                 
  4632.         err = MakeSelfAddress(&selfAddr);
  4633.         
  4634.         if (err==noErr)
  4635.             err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);
  4636.         
  4637.         if (err==noErr)
  4638.             err = SendAESetObjProp(&selTextObj, 
  4639.                                                          pTextStyles,
  4640.                                                          &result,
  4641.                                                          &selfAddr);
  4642.         
  4643.     } /* IssueStyleCommand */
  4644.     
  4645. enum {
  4646.  
  4647. ETX = 0x03, /* Enter key on keyboard or keypad */
  4648. BS  = 0x08, /* Backspace key on keyboard       */
  4649. HT  = 0x09, /* Tab key on keyboard             */
  4650. CR  = 0x0D, /* Return key on keyboard          */
  4651. ESC = 0x1B, /* Clear key on keypad             */
  4652. FS  = 0x1C, /* Left arrow key on keypad        */
  4653. GS  = 0x1D, /* Right arrow key on keypad       */
  4654. RS  = 0x1E, /* Up arrow key on keypad          */
  4655. US  = 0x1F  /* Down arrow key on keypad        */
  4656. };
  4657.  
  4658. pascal OSErr IssueSetDataObjToBufferContents(const AEDesc * theObj)
  4659.     { 
  4660.       OSErr         myErr;
  4661.         OSErr                    ignoreErr;
  4662.         AEAddressDesc theAddress;
  4663.         AppleEvent    myAppleEvent;
  4664.         AppleEvent    defReply;
  4665.  
  4666.         myErr = MakeSelfAddress(&theAddress);
  4667.         
  4668.         /* create event */
  4669.         
  4670.         if (myErr==noErr)
  4671.             myErr = AECreateAppleEvent(kAECoreSuite,
  4672.                                                                  kAESetData,
  4673.                                                                  &theAddress,
  4674.                                                                  0,
  4675.                                                                  0,
  4676.                                                                  &myAppleEvent);
  4677.             
  4678.         /* add prop obj spec to the event */
  4679.         
  4680.         if (myErr==noErr)
  4681.             myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, theObj);
  4682.         
  4683.         /* add prop data to the event */
  4684.         
  4685.         if (myErr==noErr)
  4686.             myErr = AEPutParamPtr(&myAppleEvent,
  4687.                                                         keyAEData,
  4688.                                                         typeChar,
  4689.                                                         (Ptr)gTypingBuffer,
  4690.                                                         gCharsInBuffer);
  4691.                                                         
  4692.         /* send event */
  4693.         
  4694.         if (myErr==noErr)
  4695.             myErr = AESend(&myAppleEvent,
  4696.                                          &defReply,
  4697.                                          kAENoReply+kAEDontExecute,
  4698.                                          kAENormalPriority,
  4699.                                          kAEDefaultTimeout,
  4700.                                          nil,
  4701.                                          nil);
  4702.  
  4703.         if (theAddress.dataHandle)
  4704.             ignoreErr = AEDisposeDesc(&theAddress);
  4705.             
  4706.         if (myAppleEvent.dataHandle)
  4707.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4708.             
  4709.         return(myErr);
  4710.     }
  4711.  
  4712. pascal void AddKeyToTypingBuffer(DPtr theDocument, char theKey)
  4713.     {
  4714.         OSErr myErr;
  4715.         OSErr ignoreErr;
  4716.         
  4717.         if (theKey==BS || theKey==FS || theKey==GS || theKey==RS || theKey==US)
  4718.             {
  4719.                 FlushAndRecordTypingBuffer();
  4720.                 if (theKey==BS)
  4721.                     {
  4722.                         if ((**theDocument->theText).selStart!=(**theDocument->theText).selEnd)
  4723.                             {
  4724.                                 myErr = MakeTextObj(theDocument->theWindow,
  4725.                                                                         (**theDocument->theText).selStart,
  4726.                                                                         (**theDocument->theText).selEnd,
  4727.                                                                         &gTypingTargetObject);
  4728.                             }
  4729.                         else
  4730.                             {
  4731.                                 myErr = MakeTextObj(theDocument->theWindow,
  4732.                                                                         (**theDocument->theText).selStart-1,
  4733.                                                                         (**theDocument->theText).selStart,
  4734.                                                                         &gTypingTargetObject);
  4735.                             }
  4736.                             
  4737.                     myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  4738.                         
  4739.                         ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  4740.                         
  4741.                         gTypingTargetObject.dataHandle = nil;
  4742.                     }
  4743.             }
  4744.         else
  4745.             {
  4746.                 if (gCharsInBuffer==0)
  4747.                     myErr = MakeSelectedTextObj(theDocument->theWindow,
  4748.                                                                             theDocument->theText,
  4749.                                                                             &gTypingTargetObject);
  4750.  
  4751.                 gTypingBuffer[gCharsInBuffer++] = theKey;
  4752.             }
  4753.           
  4754.     }
  4755.     
  4756. pascal void FlushAndRecordTypingBuffer(void)
  4757.     { 
  4758.       OSErr  myErr;
  4759.         OSErr  ignoreErr;
  4760.  
  4761.         if (gCharsInBuffer != 0)
  4762.             {
  4763.                 myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  4764.                 
  4765.                 if (gTypingTargetObject.dataHandle)
  4766.                     ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  4767.             }
  4768.             
  4769.         gCharsInBuffer = 0;
  4770.         gTypingTargetObject.dataHandle = 0;
  4771.     }
  4772.     
  4773. /*****************************************************************************/
  4774. /*
  4775.     Object Accessors
  4776. */
  4777.     
  4778. pascal OSErr WindowFromNullAccessor(DescType      wantClass,
  4779.                                                                     const AEDesc  *container,
  4780.                                                                         DescType      containerClass,
  4781.                                                                         DescType      form, 
  4782.                                                                         const AEDesc  *selectionData,
  4783.                                                                         AEDesc        *value,
  4784.                                                                         long          theRefCon)
  4785.     {
  4786. #pragma unused (container,theRefCon)
  4787.     
  4788.         OSErr       myErr;
  4789.         Str255      nameStr;
  4790.         WindowToken theWindow;
  4791.         short       index;
  4792.         AEDesc      resultDesc;
  4793.         
  4794.         myErr = errAEBadKeyForm;    /* or whatever */
  4795.         
  4796.         value->dataHandle     = nil;
  4797.         resultDesc.dataHandle = nil;
  4798.         
  4799.         /* 
  4800.             should only be called with wantClass = cWindow and
  4801.             with containerClass = typeNull or typeMyAppl.
  4802.             Currently accept as either formName or formAbsolutePosition
  4803.         */
  4804.         
  4805.         if ((wantClass != cWindow) || 
  4806.               ((containerClass != typeNull) && (containerClass != typeMyAppl)) ||
  4807.                !((form == formName) || (form == formAbsolutePosition)))
  4808.             return(errAEWrongDataType);
  4809.         
  4810.         if (form == formName)
  4811.             {
  4812.                 myErr     = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  4813.                 theWindow = WindowNameToWindowPtr(nameStr);
  4814.             }
  4815.         
  4816.         if (form == formAbsolutePosition)
  4817.             {
  4818.                 myErr         = GetIntegerFromDescriptor(selectionData, &index);
  4819.                 
  4820.                 if (index<0)
  4821.                     index = CountWindows()+index+1;
  4822.                     
  4823.                 theWindow = GetWindowPtrOfNthWindow(index);
  4824.             }
  4825.             
  4826.         if (myErr == noErr)
  4827.             myErr = AECreateDesc(typeMyWndw, (Ptr)&theWindow, sizeof(theWindow), value);
  4828.                 
  4829.         return(myErr);
  4830.     }    /* WindowFromNullAccessor */
  4831.  
  4832. pascal OSErr ApplicationFromNullAccessor(DescType      wantClass,
  4833.                                                                                  const AEDesc  *container,
  4834.                                                                                  DescType      containerClass,
  4835.                                                                                  DescType      form, 
  4836.                                                                                  const AEDesc  *selectionData,
  4837.                                                                                  AEDesc        *value,
  4838.                                                                                  long          theRefCon)
  4839.     {
  4840. #pragma unused (container,selectionData,theRefCon)
  4841.  
  4842.         OSErr    myErr;
  4843.         appToken theApp;
  4844.         AEDesc   resultDesc;
  4845.         
  4846.         value->dataHandle     = nil;
  4847.         resultDesc.dataHandle = nil;
  4848.         
  4849.         /* 
  4850.             should only be called with wantClass = cWindow and
  4851.             with containerClass = typeNull.
  4852.             Currently accept as either formName or formAbsolutePosition
  4853.         */
  4854.         
  4855.         if ((wantClass != cApplication) || (containerClass != typeNull) ||
  4856.               !((form == formName) || (form == formAbsolutePosition)))
  4857.             return(errAEWrongDataType);
  4858.         
  4859.         if ((form == formName) || (form == formAbsolutePosition))
  4860.             {
  4861.                 theApp.highLongOfPSN = 0;
  4862.                 theApp.lowLongOfPSN  = kCurrentProcess;
  4863.             }
  4864.             
  4865.         myErr = AECreateDesc(typeMyAppl, (Ptr)&theApp, sizeof(theApp), value);
  4866.                 
  4867.         return(myErr);
  4868.     }    /* ApplicationFromNullAccessor */
  4869.  
  4870. pascal void MoveToNonSpace(short *start, short limit, charsHandle myChars)
  4871.     /*
  4872.         Treats space,comma, full stop, ; and : as space chars
  4873.     */
  4874.     { 
  4875.         short x;
  4876.  
  4877.         while (*start<=limit) {
  4878.           x = (**myChars)[*start];
  4879.             if ( x == ' ' ||
  4880.                  x == ',' ||
  4881.                  x == '.' ||
  4882.                  x == ',' ||
  4883.                  x == ':' ||
  4884.                  x == 10  ||
  4885.                  x == 13 )
  4886.                 (*start) +=1;
  4887.             else
  4888.                 return;
  4889.         }
  4890.     }
  4891.     
  4892. pascal void MoveToSpace(short *start, short limit, charsHandle myChars)
  4893.     /*
  4894.         Treats space,comma, full stop, ; and : as space chars
  4895.     */
  4896.     { 
  4897.       short x;
  4898.         
  4899.         while (*start<=limit) {
  4900.           x = (**myChars)[*start];
  4901.             if ( x != ' ' &&
  4902.                  x != ',' &&
  4903.                  x != '.' &&
  4904.                  x != ',' &&
  4905.                  x != ':' &&
  4906.                  x != 10  &&
  4907.                  x != 13 )
  4908.                 (*start) +=1;
  4909.             else
  4910.                 return;
  4911.         }
  4912.     }
  4913.  
  4914. pascal short CountWords(TEHandle inTextHandle, short startAt, short forHowManyChars)
  4915.     {
  4916.         charsHandle myChars;
  4917.         short       start;
  4918.         short       limit;
  4919.         short       myWords;
  4920.             
  4921.         myChars  = (charsHandle)(**inTextHandle).hText;
  4922.         limit    = startAt+forHowManyChars-1;
  4923.         start    = startAt;
  4924.         myWords  = 0;
  4925.         MoveToNonSpace(&start, limit, myChars);
  4926.         while (start<=limit) {
  4927.                 myWords++;
  4928.                 MoveToSpace(&start, limit, myChars);
  4929.                 MoveToNonSpace(&start, limit, myChars);
  4930.             }
  4931.         return(myWords);
  4932.     } /* CountWords */
  4933.     
  4934. pascal void GetNthWordInfo(short    whichWord,
  4935.                                                    TEHandle inTextHandle,
  4936.                                                    short    *wordStartChar,
  4937.                                                    short    *wordLength)
  4938.     /*
  4939.         On entry:    wordStartChar is start of char range to count in
  4940.                             wordLength is number of chars to consider
  4941.                             
  4942.         On Exit : wordStartChar is start of requested word
  4943.                             wordLength is number of chars in word
  4944.     */
  4945.     {
  4946.         charsHandle myChars;
  4947.         short       start;
  4948.         short       limit;
  4949.             
  4950.         myChars  = (charsHandle)(**inTextHandle).hText;
  4951.         limit    = *wordStartChar + *wordLength-1;
  4952.         start    = *wordStartChar;
  4953.         MoveToNonSpace(&start, limit, myChars);
  4954.         while ((start<=limit) && (whichWord>0)) {
  4955.  
  4956.                 whichWord       = whichWord-1;
  4957.                 *wordStartChar  = start;
  4958.                 MoveToSpace(&start, limit, myChars);
  4959.                 *wordLength     = start- *wordStartChar;
  4960.                     
  4961.                 MoveToNonSpace(&start, limit, myChars);
  4962.             }
  4963.     } /* GetNthWordInfo */
  4964.     
  4965. pascal void GetWordInfo(short    whichWord,
  4966.                                               TEHandle inTextHandle,
  4967.                                               short    *wordStartChar,
  4968.                                               short    *wordLength)
  4969.     /*
  4970.         On wordStartChar entry is start of char range to count in
  4971.                             wordLength is number of chars to consider
  4972.                             
  4973.         On Exit : wordStartChar is start of requested word
  4974.                             wordLength is number of chars in word
  4975.     */
  4976.     {
  4977.         short noOfWords;
  4978.                     
  4979.         noOfWords = CountWords(inTextHandle, *wordStartChar, *wordLength);
  4980.         
  4981.         if (whichWord<0)
  4982.             whichWord = noOfWords + whichWord + 1;
  4983.             
  4984.         if (whichWord>noOfWords)
  4985.             {
  4986.                 *wordStartChar = *wordStartChar+*wordLength;
  4987.                 *wordLength    = 0;
  4988.             }
  4989.         else
  4990.             GetNthWordInfo(whichWord, inTextHandle, wordStartChar, wordLength);
  4991.     }
  4992.     
  4993. pascal short CountLines(TEHandle inTextHandle)
  4994.     {
  4995.         /*
  4996.             CountLines makes use of info in TERec
  4997.         */
  4998.         return((**inTextHandle).nLines);
  4999.     }
  5000.     
  5001. pascal short LineOfOffset(TEHandle theHTE, short charOffset)
  5002.     {
  5003.         short n;
  5004.     
  5005.         n = (**theHTE).nLines;
  5006.         
  5007.         while (((**theHTE).lineStarts[n-1]>charOffset) &&
  5008.                      (n>0))
  5009.              n--;
  5010.             
  5011.         return(n);
  5012.     } /* LineOfOffset */
  5013.  
  5014. pascal void GetLineInfo(short    whichLine,
  5015.                                                 TEHandle inTextHandle,
  5016.                                               short    *lineStartChar,
  5017.                                               short    *lineLength)
  5018.     {
  5019.         short       noOfLines;
  5020.         charsHandle myChars;
  5021.         
  5022.         /* Addition of lines within text object */
  5023.         short       lineOfStart;
  5024.         short       lineOfEnd;
  5025.         
  5026.         lineOfStart = LineOfOffset(inTextHandle, *lineStartChar);
  5027.         lineOfEnd   = LineOfOffset(inTextHandle, *lineStartChar+*lineLength-1);
  5028.             
  5029.         myChars   = (charsHandle)(**inTextHandle).hText;
  5030.         noOfLines = lineOfEnd - lineOfStart + 1;
  5031.         
  5032.         if (whichLine<0)
  5033.             whichLine = noOfLines + whichLine + 1;
  5034.             
  5035.         noOfLines = CountLines(inTextHandle);
  5036.         whichLine = whichLine + lineOfStart - 1; /* convert offset relative to offset absolute */
  5037.         
  5038.         /* End of addition */
  5039.         
  5040.         if (whichLine<=lineOfEnd)
  5041.             {
  5042.                 *lineStartChar = (**inTextHandle).lineStarts[whichLine-1];
  5043.                 if (whichLine==noOfLines)
  5044.                     *lineLength  = (**inTextHandle).teLength;
  5045.                 else
  5046.                     *lineLength  = (**inTextHandle).lineStarts[whichLine];
  5047.                 *lineLength    = *lineLength-*lineStartChar;
  5048.                 /*
  5049.                     Don't return CR
  5050.                 */
  5051.                 if ((**myChars)[ *lineStartChar+*lineLength-1] == 13)
  5052.                     *lineLength = *lineLength-1;
  5053.             }
  5054.         else
  5055.             {
  5056.                 if (whichLine<noOfLines)
  5057.                   *lineStartChar = (**inTextHandle).lineStarts[whichLine]; /* start of whichLine++ */
  5058.                 else
  5059.                     *lineStartChar = (**inTextHandle).teLength;
  5060.                 *lineLength    = 0;
  5061.             }
  5062.     } /* GetLineInfo */
  5063.     
  5064. pascal OSErr TextElemFromWndwAccessor(DescType     wantClass,
  5065.                                                                         const AEDesc *container,
  5066.                                                                         DescType     containerClass,
  5067.                                                                         DescType     form,
  5068.                                                                         const AEDesc *selectionData,
  5069.                                                                         AEDesc       *value,
  5070.                                                                             long         theRefCon)
  5071.   { 
  5072. #pragma unused (theRefCon)
  5073.     
  5074.         OSErr       myErr;
  5075.         OSErr       ignoreErr;
  5076.         WindowToken theWindow;
  5077.         Size        actSize;
  5078.         long        index;
  5079.         TextToken   theTextToken;
  5080.         AERecord    selectionRecord;
  5081.         TextToken   startText;
  5082.         TextToken   stopText;
  5083.         DescType    returnedType;
  5084.         AEDesc      windDesc;
  5085.         TEHandle    theHTE;
  5086.         DPtr        theDocument;
  5087.         short       wordStartChar;
  5088.         short       wordLength;
  5089.     
  5090.         myErr = -1700;    /* or whatever */
  5091.         
  5092.         selectionRecord.dataHandle = nil;
  5093.         
  5094.         /* do some checking for robustness' sake */
  5095.         
  5096.         if ((containerClass != cWindow) ||
  5097.               ((wantClass != cChar) && 
  5098.                  (wantClass != cSpot) && 
  5099.                  (wantClass != cWord) &&
  5100.                  (wantClass != cLine)    ) ||
  5101.               ((form!=formRange) && (form!=formAbsolutePosition)))
  5102.             return(errAEWrongDataType);
  5103.             
  5104.         /* let's get the window which contains the text element */
  5105.         
  5106.         myErr = AECoerceDesc(container, typeMyWndw, &windDesc);
  5107.         GetRawDataFromDescriptor(    &windDesc,
  5108.                                                             (Ptr)&theWindow,
  5109.                                                             sizeof(theWindow),
  5110.                                                             &actSize);
  5111.         ignoreErr = AEDisposeDesc(&windDesc);
  5112.                                                             
  5113.         if (theWindow==nil)
  5114.             myErr = errAEIllegalIndex;
  5115.         else
  5116.             {
  5117.                                                                                     
  5118.                 theTextToken.tokenWindow = theWindow;
  5119.                 
  5120.                 theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  5121.                 theHTE      = theDocument->theText;
  5122.                 
  5123.                 if (form == formAbsolutePosition)
  5124.                     {
  5125.                         myErr = GetLongIntFromDescriptor(selectionData, &index);
  5126.                         
  5127.                         if (wantClass==cSpot)
  5128.                             {
  5129.                               if (index<0)
  5130.                                     theTextToken.tokenOffset = (**theHTE).teLength+index+2; /* Past last char */
  5131.                                 else
  5132.                                     theTextToken.tokenOffset = index;
  5133.                                     
  5134.                                 theTextToken.tokenLength = 0;
  5135.                             }
  5136.                             
  5137.                         if (wantClass==cChar)
  5138.                             {
  5139.                                 if (index<0)
  5140.                                     theTextToken.tokenOffset = (**theHTE).teLength+index+1;
  5141.                                 else
  5142.                                   theTextToken.tokenOffset = index;
  5143.                                     
  5144.                                 theTextToken.tokenLength = 1;
  5145.                             }
  5146.                         
  5147.                         if (wantClass==cWord)
  5148.                             {
  5149.                                 wordStartChar = 0;
  5150.                                 wordLength    = (**theHTE).teLength;
  5151.                                 GetWordInfo(index, theHTE, &wordStartChar, &wordLength); /* zero based */
  5152.                                 theTextToken.tokenOffset = wordStartChar+1;
  5153.                                 theTextToken.tokenLength = wordLength;
  5154.                             }
  5155.                             
  5156.                         if (wantClass==cLine)
  5157.                             {
  5158.                                 wordStartChar = 0;
  5159.                                 wordLength    = (**theHTE).teLength;
  5160.                                 GetLineInfo(index, theHTE, &wordStartChar, &wordLength); /* zero based */
  5161.                                 theTextToken.tokenOffset = wordStartChar+1;
  5162.                                 theTextToken.tokenLength = wordLength;
  5163.                             }
  5164.                     }    /* of formAbsolutePosition */
  5165.                 
  5166.                 if (form == formRange)
  5167.                     {
  5168.                         /* coerce the selection data into an AERecord */
  5169.                         
  5170.                          myErr = AECoerceDesc(selectionData, typeAERecord, &selectionRecord);
  5171.                         
  5172.                         /* get the start object as a text token - 
  5173.                                 this will reenter this proc but as formAbsolutePosition via the coercion handler*/
  5174.                         
  5175.                         myErr =  AEGetKeyPtr(&selectionRecord,
  5176.                                                                  keyAERangeStart,
  5177.                                                                  typeMyText,
  5178.                                                                  &returnedType,
  5179.                                                                  (Ptr)&startText,
  5180.                                                                  sizeof(startText),
  5181.                                                                  &actSize);
  5182.                         
  5183.                         /* now do the same for the stop object */
  5184.                         
  5185.                         if (myErr==noErr)
  5186.                             myErr =  AEGetKeyPtr(&selectionRecord,
  5187.                                                                      keyAERangeStop,
  5188.                                                                      typeMyText,
  5189.                                                                      &returnedType,
  5190.                                                                      (Ptr)&stopText,
  5191.                                                                      sizeof(stopText),
  5192.                                                                      &actSize);
  5193.                         
  5194.                         if (myErr==noErr)
  5195.                             if ((theTextToken.tokenWindow != stopText.tokenWindow) ||
  5196.                                   (theTextToken.tokenWindow != startText.tokenWindow))
  5197.                                 myErr = errAECorruptData;    /* or whatever ????*/
  5198.                             
  5199.                         theTextToken.tokenOffset  = startText.tokenOffset;            
  5200.                         theTextToken.tokenLength  = stopText.tokenOffset +
  5201.                                                                                  stopText.tokenLength - 
  5202.                                                                                  startText.tokenOffset;
  5203.                                                                                  
  5204.                         if (theTextToken.tokenLength<0)
  5205.                             myErr = errAECorruptData;    /* or whatever */
  5206.                                                                              
  5207.                         ignoreErr = AEDisposeDesc(&selectionRecord);
  5208.                         
  5209.                     }    /* of formRange */
  5210.             }
  5211.         
  5212.         /* return theTextToken in a descriptor */
  5213.         
  5214.         if (myErr==noErr)
  5215.             myErr = AECreateDesc(typeMyText,
  5216.                                                      (Ptr)&theTextToken,
  5217.                                                      sizeof(theTextToken),
  5218.                                                      value);
  5219.                 
  5220.         return(myErr);
  5221.     }    /* TextElemFromWndwAccessor */
  5222.  
  5223. pascal OSErr TextElemFromTextAccessor(DescType     wantClass,
  5224.                                                                         const AEDesc *container,
  5225.                                                                         DescType     containerClass,
  5226.                                                                         DescType     form,
  5227.                                                                         const AEDesc *selectionData,
  5228.                                                                         AEDesc       *value,
  5229.                                                                         long         theRefCon)
  5230.     { 
  5231. #pragma unused (theRefCon, containerClass)
  5232.  
  5233.         OSErr       myErr;
  5234.         OSErr       ignoreErr;
  5235.         Size        actSize;
  5236.         long        index;
  5237.         TextToken   theTextToken;
  5238.         AERecord    selectionRecord;
  5239.         TextToken   startText;
  5240.         TextToken   stopText;
  5241.         DescType    returnedType;
  5242.         AEDesc      textDesc;
  5243.         TEHandle    theHTE;
  5244.         short       wordStartChar;
  5245.         short       wordLength;
  5246.         DPtr        theDocument;
  5247.     
  5248.         myErr = -1700;    /* or whatever */
  5249.             
  5250.         /* do some checking for robustness' sake */
  5251.         
  5252.         if (((wantClass != cChar) && 
  5253.                  (wantClass != cSpot) && 
  5254.                  (wantClass != cLine) && 
  5255.                  (wantClass != cWord)) ||
  5256.               ((form != formAbsolutePosition) && (form != formRange)))
  5257.             return(errAEWrongDataType);
  5258.         
  5259.         /* let's get the src text */
  5260.         
  5261.         myErr = AECoerceDesc(container, typeMyText, &textDesc);
  5262.         GetRawDataFromDescriptor(    &textDesc,
  5263.                                                             (Ptr)&theTextToken,
  5264.                                                             sizeof(theTextToken),
  5265.                                                             &actSize);
  5266.                                                             
  5267.         ignoreErr = AEDisposeDesc(&textDesc);
  5268.                                                             
  5269.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  5270.         theHTE      = theDocument->theText;
  5271.         
  5272.         if (form == formAbsolutePosition)
  5273.             {
  5274.                 myErr = GetLongIntFromDescriptor(selectionData, &index);
  5275.                 
  5276.                 if (wantClass==cSpot)
  5277.                     {
  5278.                         if (index<0)
  5279.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index+1+theTextToken.tokenLength;
  5280.                         else
  5281.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index-1;
  5282.                         theTextToken.tokenLength = 0;
  5283.                     }
  5284.                     
  5285.                 if (wantClass==cChar)
  5286.                     {
  5287.                         if (index<0)
  5288.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index+1+theTextToken.tokenLength;
  5289.                         else
  5290.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index-1;
  5291.                         theTextToken.tokenLength = 1;
  5292.                     }
  5293.                                     
  5294.                 if (wantClass==cWord)
  5295.                     {
  5296.                         wordStartChar = theTextToken.tokenOffset-1;
  5297.                         wordLength    = theTextToken.tokenLength;
  5298.                         
  5299.                         GetWordInfo(index, theHTE, &wordStartChar, &wordLength);/*zero based*/
  5300.                         
  5301.                         theTextToken.tokenOffset = wordStartChar+1;
  5302.                         theTextToken.tokenLength = wordLength;
  5303.                     }
  5304.                     
  5305.                 if (wantClass==cLine)
  5306.                     {
  5307.                         wordStartChar = theTextToken.tokenOffset-1;
  5308.                         wordLength    = theTextToken.tokenLength;
  5309.                         
  5310.                         GetLineInfo(index, theHTE, &wordStartChar, &wordLength);
  5311.                         
  5312.                         theTextToken.tokenOffset = wordStartChar+1;
  5313.                         theTextToken.tokenLength = wordLength;
  5314.                     }
  5315.     
  5316.             }    /* of formAbsolutePosition */
  5317.         
  5318.         
  5319.         if (form == formRange)
  5320.             {
  5321.                 /* coerce the selection data into an AERecord */
  5322.                 
  5323.                  myErr = AECoerceDesc(selectionData, typeAERecord, &selectionRecord);
  5324.                 
  5325.                 /* get the start object as a text token - 
  5326.                         this will reenter this proc but as formAbsolutePosition via the coercion handler*/
  5327.                 
  5328.                 myErr =  AEGetKeyPtr(&selectionRecord,
  5329.                                                          keyAERangeStart,
  5330.                                                          typeMyText,
  5331.                                                          &returnedType,
  5332.                                                          (Ptr)&startText,
  5333.                                                          sizeof(startText),
  5334.                                                          &actSize);
  5335.                 
  5336.                 /* now do the same for the stop object */
  5337.                 
  5338.                 if (myErr==noErr)
  5339.                     myErr =  AEGetKeyPtr(&selectionRecord,
  5340.                                                              keyAERangeStop,
  5341.                                                              typeMyText,
  5342.                                                              &returnedType,
  5343.                                                              (Ptr)&stopText,
  5344.                                                              sizeof(stopText),
  5345.                                                              &actSize);
  5346.                 
  5347.                 if (myErr==noErr)
  5348.                     if ((theTextToken.tokenWindow != stopText.tokenWindow) ||
  5349.                           (theTextToken.tokenWindow != startText.tokenWindow))
  5350.                         myErr = errAECorruptData;    /* or whatever */
  5351.                     
  5352.                 theTextToken.tokenOffset  = startText.tokenOffset;            
  5353.                 theTextToken.tokenLength  = stopText.tokenOffset +
  5354.                                                                          stopText.tokenLength - 
  5355.                                                                          startText.tokenOffset;
  5356.                                                                      
  5357.                 ignoreErr = AEDisposeDesc(&selectionRecord);
  5358.             }    /* of formRange */
  5359.             
  5360.         /* return theTextToken in a descriptor */
  5361.         
  5362.         myErr = AECreateDesc( typeMyText,
  5363.                                                     (Ptr)&theTextToken,
  5364.                                                     sizeof(theTextToken),
  5365.                                                     value);
  5366.             
  5367.         return(myErr);
  5368.     }    /* TextElemFromTextAccessor */
  5369.  
  5370. pascal OSErr PropertyFromTextAccessor(DescType     wantClass,
  5371.                                                                             const AEDesc *container,
  5372.                                                                             DescType     containerClass,
  5373.                                                                             DescType     form, 
  5374.                                                                             const AEDesc *selectionData,
  5375.                                                                             AEDesc       *value,
  5376.                                                                             long         theRefCon)
  5377.   {
  5378. #pragma unused (theRefCon, containerClass)
  5379.     
  5380.         OSErr         myErr;
  5381.         OSErr         ignoreErr;
  5382.         TextToken     theTextToken;
  5383.         DescType      theProperty;
  5384.         AEDesc        textDesc;
  5385.         AEDesc        propDesc;
  5386.         Size          actualSize;
  5387.         textPropToken myTextProp;
  5388.             
  5389.         value->dataHandle   = nil;
  5390.         textDesc.dataHandle = nil;
  5391.         propDesc.dataHandle = nil;
  5392.         
  5393.         if ((wantClass != cProperty) ||
  5394.                 (form != formPropertyID))
  5395.             {
  5396.                 return(errAEWrongDataType);
  5397.             }
  5398.         
  5399.         /* get the text token */
  5400.         myErr = AECoerceDesc(container, typeMyText, &textDesc);
  5401.         GetRawDataFromDescriptor(    &textDesc,
  5402.                                                             (Ptr)&theTextToken,
  5403.                                                             sizeof(theTextToken),
  5404.                                                             &actualSize);
  5405.                 
  5406.         /* get the property */
  5407.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5408.         GetRawDataFromDescriptor(&propDesc,
  5409.                                                          (Ptr)&theProperty,
  5410.                                                          sizeof(theProperty),
  5411.                                                          &actualSize);
  5412.         /*
  5413.             Combine the two into single token
  5414.         */
  5415.         myTextProp.propertyTextToken = theTextToken;
  5416.         myTextProp.propertyProperty  = theProperty;
  5417.         
  5418.         myErr = AECreateDesc(typeMyTextProp,
  5419.                                                  (Ptr)&myTextProp,
  5420.                                                  sizeof(myTextProp),
  5421.                                                  value);
  5422.             
  5423.         if (textDesc.dataHandle)
  5424.             ignoreErr = AEDisposeDesc(&textDesc);
  5425.             
  5426.         if (propDesc.dataHandle)
  5427.             ignoreErr = AEDisposeDesc(&propDesc);
  5428.             
  5429.         return(myErr);
  5430. }    /* PropertyFromTextAccessor */
  5431.  
  5432. pascal OSErr PropertyFromWndwAccessor(DescType     wantClass,
  5433.                                                                         const AEDesc *container,
  5434.                                                                         DescType     containerClass,
  5435.                                                                         DescType     form, 
  5436.                                                                         const AEDesc *selectionData,
  5437.                                                                         AEDesc       *value,
  5438.                                                                         long         theRefCon)
  5439.   { 
  5440. #pragma unused (theRefCon, containerClass)
  5441.     
  5442.         OSErr           myErr;
  5443.         OSErr           ignoreErr;
  5444.         WindowToken     theWindowToken;
  5445.         DescType        theProperty;
  5446.         AEDesc          windowDesc;
  5447.         AEDesc          propDesc;
  5448.         Size            actualSize;
  5449.         windowPropToken myWindowProp;
  5450.         
  5451.         value->dataHandle     = nil;
  5452.         windowDesc.dataHandle = nil;
  5453.         propDesc.dataHandle   = nil;
  5454.         
  5455.         if ((wantClass != cProperty) ||
  5456.               (form != formPropertyID))
  5457.             {
  5458.                 return(errAEWrongDataType);
  5459.             }
  5460.         
  5461.         /* get the window token - it's the container */
  5462.         
  5463.         myErr = AECoerceDesc(container, typeMyWndw, &windowDesc);
  5464.         GetRawDataFromDescriptor(    &windowDesc,
  5465.                                                             (Ptr)&theWindowToken,
  5466.                                                             sizeof(theWindowToken),
  5467.                                                             &actualSize);
  5468.                                                             
  5469.         /* Check the window exists */
  5470.         
  5471.         if (theWindowToken==nil)
  5472.             myErr = errAEIllegalIndex;
  5473.         else
  5474.             {
  5475.             
  5476.                 /* get the property - it's in the selection data */
  5477.                 
  5478.                 myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5479.                 GetRawDataFromDescriptor(    &propDesc,
  5480.                                                                     (Ptr)&theProperty,
  5481.                                                                     sizeof(theProperty),
  5482.                                                                     &actualSize);
  5483.                 /*
  5484.                     Combine the two into single token
  5485.                 */
  5486.                 myWindowProp.tokenWindowToken = theWindowToken;
  5487.                 myWindowProp.tokenProperty    = theProperty;
  5488.                 
  5489.                 myErr = AECreateDesc( typeMyWindowProp,
  5490.                                                             (Ptr)&myWindowProp,
  5491.                                                             sizeof(myWindowProp),
  5492.                                                             value);
  5493.             }
  5494.             
  5495.         if (windowDesc.dataHandle)
  5496.             ignoreErr = AEDisposeDesc(&windowDesc);
  5497.             
  5498.         if (propDesc.dataHandle)
  5499.             ignoreErr = AEDisposeDesc(&propDesc);
  5500.             
  5501.         return(myErr);
  5502.   }    /* PropertyFromWndwAccessor */
  5503.  
  5504.  
  5505. pascal OSErr PropertyFromWinPropertyAccessor(DescType     wantClass,
  5506.                                                                                        const AEDesc *container,
  5507.                                                                                        DescType     containerClass,
  5508.                                                                                        DescType     form,
  5509.                                                                                        const AEDesc *selectionData,
  5510.                                                                                        AEDesc       *value,
  5511.                                                                                        long         theRefCon)
  5512. {
  5513. #pragma unused (theRefCon, containerClass)
  5514.  
  5515.    OSErr           myErr;
  5516.    OSErr           ignoreErr;
  5517.    windowPropToken theWindowPropToken;
  5518.    AEDesc          newDesc, propDesc;
  5519.    Size            tokenSize;
  5520.    DPtr            theDocument;
  5521.    TextToken       theTextToken;
  5522.    DescType        theProperty;
  5523.    Size            actualSize;
  5524.    textPropToken   myTextProp;
  5525.  
  5526.         value->dataHandle     = nil;
  5527.         propDesc.dataHandle   = nil;
  5528.  
  5529.         if ((wantClass != formPropertyID) || (form != formPropertyID))
  5530.         {
  5531.             return(errAEWrongDataType);
  5532.         }
  5533.  
  5534.    /* the container is a window property token. Get the token for this */
  5535.    /* and check that it is valid to get a property of this property */
  5536.  
  5537.         myErr = AECoerceDesc(container, typeMyWindowProp, &newDesc);
  5538.         
  5539.         if (myErr)
  5540.             return(myErr);
  5541.  
  5542.         GetRawDataFromDescriptor(&newDesc,
  5543.                                                         (Ptr)&theWindowPropToken,
  5544.                                                         sizeof(theWindowPropToken),
  5545.                                                         &tokenSize);
  5546.  
  5547.            /* if the property is pSelection, we then want to convert this to a text token */
  5548.            /* and then return a text property token */
  5549.  
  5550.         if (theWindowPropToken.tokenProperty == pSelection)
  5551.         {
  5552.             /* now get the property- it's in the selection data */
  5553.             myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5554.             GetRawDataFromDescriptor(&propDesc,
  5555.                                                             (Ptr)&theProperty,
  5556.                                                             sizeof(theProperty),
  5557.                                                             &actualSize);
  5558.  
  5559.             if (pContents == theProperty)
  5560.             {
  5561.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  5562.                 
  5563.                 /* build a text token to represent the selection */
  5564.                 theTextToken.tokenOffset = (**(theDocument->theText)).selStart + 1;
  5565.                 theTextToken.tokenLength = ((**(theDocument->theText)).selEnd -
  5566.                                         (**(theDocument->theText)).selStart);
  5567.                 theTextToken.tokenWindow = theWindowPropToken.tokenWindowToken;
  5568.                 
  5569.                 /* Combine the two into single token */
  5570.                 myTextProp.propertyTextToken = theTextToken;
  5571.                 myTextProp.propertyProperty  = theProperty;
  5572.                 
  5573.                 myErr = AECreateDesc(typeMyTextProp,(Ptr)&myTextProp,sizeof(myTextProp),value);
  5574.             }
  5575.             else
  5576.                 myErr = errAEWrongDataType;
  5577.             
  5578.             if (propDesc.dataHandle) ignoreErr = AEDisposeDesc(&propDesc);
  5579.             if (newDesc.dataHandle)  ignoreErr = AEDisposeDesc(&newDesc);
  5580.             
  5581.             return myErr;
  5582.         }
  5583.  
  5584.         if (newDesc.dataHandle)  ignoreErr = AEDisposeDesc(&newDesc);
  5585.  
  5586.         return errAEWrongDataType;
  5587. }
  5588.  
  5589.  
  5590. pascal OSErr PropertyFromApplAccessor(DescType     wantClass,
  5591.                                                                             const AEDesc *container,
  5592.                                                                             DescType     containerClass,
  5593.                                                                             DescType     form, 
  5594.                                                                             const AEDesc *selectionData,
  5595.                                                                             AEDesc       *value,
  5596.                                                                             long         theRefCon)
  5597.   {
  5598. #pragma unused (theRefCon, containerClass)
  5599.     
  5600.         OSErr         myErr;
  5601.         OSErr         ignoreErr;
  5602.         appToken      theApplToken;
  5603.         DescType      theProperty;
  5604.         AEDesc        applDesc;
  5605.         AEDesc        propDesc;
  5606.         Size          actualSize;
  5607.         applPropToken myApplProp;
  5608.             
  5609.         value->dataHandle     = nil;
  5610.         applDesc.dataHandle   = nil;
  5611.         propDesc.dataHandle   = nil;
  5612.         
  5613.         if ((wantClass != cProperty) ||
  5614.               (form != formPropertyID))
  5615.             {
  5616.                 return(errAEWrongDataType);
  5617.             }
  5618.         
  5619.         /* get the application token - it's the container */
  5620.         
  5621.         myErr = AECoerceDesc(container, typeMyAppl, &applDesc);
  5622.         GetRawDataFromDescriptor(&applDesc,
  5623.                                                          (Ptr)&theApplToken,
  5624.                                                          sizeof(theApplToken),
  5625.                                                          &actualSize);
  5626.                 
  5627.         /* get the property - it's in the selection data */
  5628.         
  5629.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5630.         GetRawDataFromDescriptor(&propDesc,
  5631.                                                          (Ptr)&theProperty,
  5632.                                                          sizeof(theProperty),
  5633.                                                          &actualSize);
  5634.         /*
  5635.             Combine the two into single token
  5636.         */
  5637.         myApplProp.tokenApplToken    = theApplToken;
  5638.         myApplProp.tokenApplProperty = theProperty;
  5639.         
  5640.         myErr = AECreateDesc(typeMyApplProp,
  5641.                                                  (Ptr)&myApplProp,
  5642.                                                  sizeof(myApplProp),
  5643.                                                  value);
  5644.             
  5645.         if (applDesc.dataHandle)
  5646.             ignoreErr = AEDisposeDesc(&applDesc);
  5647.             
  5648.         if (propDesc.dataHandle)
  5649.             ignoreErr = AEDisposeDesc(&propDesc);
  5650.             
  5651.         return(myErr);
  5652. }    /* PropertyFromApplAccessor */
  5653.  
  5654. pascal OSErr MenuNameToMenuToken(const Str255 theName, MenuToken *theToken)
  5655.     {
  5656.         short   index;
  5657.         
  5658.         for (index=appleM; index<=kLastMenu; index++)
  5659.          {
  5660.                 if (IUEqualString(theName, (**(myMenus[index])).menuData)==0)
  5661.                   {
  5662.                         theToken->theTokenMenu = myMenus[index];
  5663.                         theToken->theTokenID   = index+appleID;
  5664.                         return(noErr);
  5665.                     }
  5666.          }
  5667.         return(errAEIllegalIndex);
  5668.     }
  5669.  
  5670. pascal OSErr MenuFromNullAccessor(DescType      wantClass,
  5671.                                                                     const AEDesc  *container,
  5672.                                                                     DescType      containerClass,
  5673.                                                                     DescType      form, 
  5674.                                                                     const AEDesc  *selectionData,
  5675.                                                                     AEDesc        *value,
  5676.                                                                     long          theRefCon)
  5677.     {
  5678. #pragma unused (container,theRefCon)
  5679.     
  5680.         OSErr       myErr;
  5681.         Str255      nameStr;
  5682.         MenuToken   theMenu;
  5683.         short       index;
  5684.         AEDesc      resultDesc;
  5685.         
  5686.         myErr = errAEBadKeyForm;    /* or whatever */
  5687.         
  5688.         value->dataHandle     = nil;
  5689.         resultDesc.dataHandle = nil;
  5690.         
  5691.         /* 
  5692.             should only be called with wantClass = cMenu and
  5693.             with containerClass = typeNull or typeMyAppl.
  5694.             Currently accept as either formName or formAbsolutePosition
  5695.         */
  5696.         
  5697.         if ((wantClass != cMenu) || 
  5698.               ((containerClass != typeNull) && (containerClass != typeMyAppl)) ||
  5699.                !((form == formName) || (form == formAbsolutePosition)))
  5700.             return(errAEWrongDataType);
  5701.         
  5702.         if (form == formName)
  5703.             {
  5704.                 myErr = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  5705.                 myErr = MenuNameToMenuToken(nameStr, &theMenu);
  5706.             }
  5707.         
  5708.         if (form == formAbsolutePosition)
  5709.             {
  5710.                 myErr     = GetIntegerFromDescriptor(selectionData, &index);
  5711.                 if (index<0)
  5712.                     index = kLastMenu + index + 1;
  5713.                     
  5714.                 if (index>0 && index<=kLastMenu+1)
  5715.                     {
  5716.                         theMenu.theTokenMenu = myMenus[index-1];
  5717.                         theMenu.theTokenID   = index-1+appleID;
  5718.                     }
  5719.                 else
  5720.               myErr = errAEIllegalIndex;    /* or whatever */
  5721.             }
  5722.             
  5723.         if (myErr == noErr)
  5724.             myErr = AECreateDesc(typeMyMenu, (Ptr)&theMenu, sizeof(theMenu), value);
  5725.                 
  5726.         return(myErr);
  5727.     }    /* MenuFromNullAccessor */
  5728.  
  5729. pascal OSErr PropertyFromMenuAccessor(DescType     wantClass,
  5730.                                                                             const AEDesc *container,
  5731.                                                                             DescType     containerClass,
  5732.                                                                             DescType     form, 
  5733.                                                                             const AEDesc *selectionData,
  5734.                                                                             AEDesc       *value,
  5735.                                                                             long         theRefCon)
  5736.   {
  5737. #pragma unused (theRefCon, containerClass)
  5738.     
  5739.         OSErr         myErr;
  5740.         OSErr         ignoreErr;
  5741.         MenuToken     theMenuToken;
  5742.         DescType      theProperty;
  5743.         AEDesc        menuDesc;
  5744.         AEDesc        propDesc;
  5745.         Size          actualSize;
  5746.         MenuPropToken myMenuProp;
  5747.             
  5748.         value->dataHandle     = nil;
  5749.         menuDesc.dataHandle   = nil;
  5750.         propDesc.dataHandle   = nil;
  5751.         
  5752.         if ((wantClass != cProperty) ||
  5753.               (form != formPropertyID))
  5754.             {
  5755.                 return(errAEWrongDataType);
  5756.             }
  5757.         
  5758.         /* get the menu token - it's the container */
  5759.         
  5760.         myErr = AECoerceDesc(container, typeMyMenu, &menuDesc);
  5761.         GetRawDataFromDescriptor(&menuDesc,
  5762.                                                          (Ptr)&theMenuToken,
  5763.                                                          sizeof(theMenuToken),
  5764.                                                          &actualSize);
  5765.                 
  5766.         /* get the property - it's in the selection data */
  5767.         
  5768.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5769.         GetRawDataFromDescriptor(&propDesc,
  5770.                                                          (Ptr)&theProperty,
  5771.                                                          sizeof(theProperty),
  5772.                                                          &actualSize);
  5773.         /*
  5774.             Combine the two into single token
  5775.         */
  5776.         myMenuProp.theMenuToken = theMenuToken;
  5777.         myMenuProp.theMenuProp  = theProperty;
  5778.         
  5779.         myErr = AECreateDesc(typeMyMenuProp,
  5780.                                                  (Ptr)&myMenuProp,
  5781.                                                  sizeof(myMenuProp),
  5782.                                                  value);
  5783.             
  5784.         if (menuDesc.dataHandle)
  5785.             ignoreErr = AEDisposeDesc(&menuDesc);
  5786.             
  5787.         if (propDesc.dataHandle)
  5788.             ignoreErr = AEDisposeDesc(&propDesc);
  5789.             
  5790.         return(myErr);
  5791. }    /* PropertyFromMenuAccessor */
  5792.  
  5793. pascal OSErr PropertyFromMenuItemAccessor(DescType     wantClass,
  5794.                                                                                     const AEDesc *container,
  5795.                                                                                     DescType     containerClass,
  5796.                                                                                     DescType     form, 
  5797.                                                                                     const AEDesc *selectionData,
  5798.                                                                                     AEDesc       *value,
  5799.                                                                                     long         theRefCon)
  5800.   {
  5801. #pragma unused (theRefCon, containerClass)
  5802.     
  5803.         OSErr         myErr;
  5804.         OSErr         ignoreErr;
  5805.         MenuItemToken theMenuItemToken;
  5806.         DescType      theProperty;
  5807.         AEDesc        itemDesc;
  5808.         AEDesc        propDesc;
  5809.         Size          actualSize;
  5810.         MenuItemPropToken myItemProp;
  5811.             
  5812.         value->dataHandle     = nil;
  5813.         itemDesc.dataHandle   = nil;
  5814.         propDesc.dataHandle   = nil;
  5815.         
  5816.         if ((wantClass != cProperty) ||
  5817.               (form != formPropertyID))
  5818.             {
  5819.                 return(errAEWrongDataType);
  5820.             }
  5821.         
  5822.         /* get the menu token - it's the container */
  5823.         
  5824.         myErr = AECoerceDesc(container, typeMyMenuItem, &itemDesc);
  5825.         GetRawDataFromDescriptor(&itemDesc,
  5826.                                                          (Ptr)&theMenuItemToken,
  5827.                                                          sizeof(theMenuItemToken),
  5828.                                                          &actualSize);
  5829.                 
  5830.         /* get the property - it's in the selection data */
  5831.         
  5832.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5833.         GetRawDataFromDescriptor(&propDesc,
  5834.                                                          (Ptr)&theProperty,
  5835.                                                          sizeof(theProperty),
  5836.                                                          &actualSize);
  5837.         /*
  5838.             Combine the two into single token
  5839.         */
  5840.         myItemProp.theItemToken  = theMenuItemToken;
  5841.         myItemProp.theItemProp   = theProperty;
  5842.         
  5843.         myErr = AECreateDesc(typeMyItemProp,
  5844.                                                  (Ptr)&myItemProp,
  5845.                                                  sizeof(myItemProp),
  5846.                                                  value);
  5847.             
  5848.         if (itemDesc.dataHandle)
  5849.             ignoreErr = AEDisposeDesc(&itemDesc);
  5850.             
  5851.         if (propDesc.dataHandle)
  5852.             ignoreErr = AEDisposeDesc(&propDesc);
  5853.             
  5854.         return(myErr);
  5855. }    /* PropertyFromMenuItemAccessor */
  5856.  
  5857. pascal OSErr ItemNameToItemIndex(const Str255 theName, MenuHandle theMenu, short *theIndex)
  5858.     {
  5859.         short   index;
  5860.         short   maxItems;
  5861.         Str255  menuName;
  5862.         
  5863.         maxItems = CountMItems(theMenu);
  5864.         
  5865.         for (index=1; index<=maxItems; index++)
  5866.             {
  5867.                 GetItem(theMenu, index, menuName);
  5868.                 if (IUEqualString(theName, menuName)==0)
  5869.                     {
  5870.                         *theIndex = index;
  5871.                         return(noErr);
  5872.                     }
  5873.             }
  5874.         return(errAEIllegalIndex);
  5875.     }
  5876.     
  5877. pascal OSErr MenuItemFromMenuAccessor(DescType     wantClass,
  5878.                                                                             const AEDesc *container,
  5879.                                                                             DescType     containerClass,
  5880.                                                                             DescType     form, 
  5881.                                                                             const AEDesc *selectionData,
  5882.                                                                             AEDesc       *value,
  5883.                                                                             long         theRefCon)
  5884.   {
  5885. #pragma unused (theRefCon)
  5886.     
  5887.         OSErr         myErr;
  5888.         OSErr         ignoreErr;
  5889.         MenuItemToken theMenuItemToken;
  5890.         MenuToken     theMenuToken;
  5891.         AEDesc        menuDesc;
  5892.         Size          actualSize;
  5893.         Str255        nameStr;
  5894.         short         maxItems;
  5895.         short         index;
  5896.             
  5897.         value->dataHandle     = nil;
  5898.         menuDesc.dataHandle   = nil;
  5899.         
  5900.         if ((wantClass != cMenuItem) || (containerClass != cMenu) ||
  5901.               ((form != formAbsolutePosition) && (form != formName)))
  5902.             {
  5903.                 return(errAEWrongDataType);
  5904.             }
  5905.         
  5906.         /* get the menu token - it's the container */
  5907.         
  5908.         myErr = AECoerceDesc(container, typeMyMenu, &menuDesc);
  5909.         GetRawDataFromDescriptor(&menuDesc,
  5910.                                                          (Ptr)&theMenuToken,
  5911.                                                          sizeof(theMenuToken),
  5912.                                                          &actualSize);
  5913.                 
  5914.         if (form==formAbsolutePosition)
  5915.             {
  5916.                 myErr = GetIntegerFromDescriptor(selectionData, &index);
  5917.                 maxItems = CountMItems(theMenuToken.theTokenMenu);
  5918.                 
  5919.                 if (index<0)
  5920.                     index = maxItems + index + 1;
  5921.                     
  5922.                 if ((index<1) || (index>maxItems))
  5923.                   myErr = errAEIllegalIndex;
  5924.             }
  5925.             
  5926.         if (form == formName)
  5927.             {
  5928.                 myErr  = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  5929.                 myErr  = ItemNameToItemIndex(nameStr, theMenuToken.theTokenMenu, &index);
  5930.             }
  5931.         
  5932.         /*
  5933.             Combine the two into single token
  5934.         */
  5935.     
  5936.         theMenuItemToken.theMenuToken  = theMenuToken;
  5937.         theMenuItemToken.theTokenItem  = index;
  5938.         
  5939.         if (myErr==noErr)
  5940.             myErr = AECreateDesc(typeMyMenuItem,
  5941.                                                      (Ptr)&theMenuItemToken,
  5942.                                                      sizeof(theMenuItemToken),
  5943.                                                      value);
  5944.             
  5945.         if (menuDesc.dataHandle)
  5946.             ignoreErr = AEDisposeDesc(&menuDesc);
  5947.             
  5948.         return(myErr);
  5949. }    /* MenuItemFromMenuAccessor */
  5950.  
  5951. /*******************************************************************************/
  5952. /*
  5953.     Stuff for counting objects
  5954. */
  5955.  
  5956. pascal OSErr MyCountProc(DescType     desiredType,
  5957.                                                DescType     containerClass,
  5958.                                                const AEDesc *container,
  5959.                                                long         *result)
  5960.                                             
  5961. /* so far all I count is:
  5962.   (1) the number of active windows in the app;
  5963.   (2) the number of words in a window 
  5964. */
  5965.  
  5966.     {   
  5967.         OSErr       myErr;
  5968.         OSErr       ignoreErr;
  5969.         WindowToken theWindowToken;
  5970.         DPtr        theDocument;
  5971.         TEHandle    theHTE;
  5972.         AEDesc      newDesc;
  5973.         short       wordStart;
  5974.         short       wordLength;
  5975.         Size        tokenSize;
  5976.         TextToken   theTextToken;
  5977.             
  5978.         *result = -1;    /* easily recognized illegal value */
  5979.             
  5980.         myErr = errAEWrongDataType;
  5981.         
  5982.         if (desiredType == cWindow)
  5983.             {
  5984.                 if ((containerClass == typeNull) ||
  5985.                       (containerClass == cApplication))
  5986.                     *result = CountWindows();
  5987.             }
  5988.             
  5989.         if ((desiredType == cWord) ||
  5990.               (desiredType == cLine) ||
  5991.               (desiredType == cChar))
  5992.             {
  5993.                 myErr = AECoerceDesc(container, typeMyWndw, &newDesc);
  5994.                 if (newDesc.descriptorType!=typeNull)
  5995.                     {
  5996.                         GetRawDataFromDescriptor(&newDesc,
  5997.                                                                          (Ptr)&theWindowToken,
  5998.                                                                          sizeof(theWindowToken),
  5999.                                                                          &tokenSize);
  6000.                                                                          
  6001.                         ignoreErr = AEDisposeDesc(&newDesc);
  6002.                                                                          
  6003.                         if (theWindowToken==nil)
  6004.                             myErr = errAEIllegalIndex;
  6005.                         else
  6006.                             {
  6007.                         
  6008.                                 theDocument = DPtrFromWindowPtr(theWindowToken);
  6009.                                 theHTE      = theDocument->theText;
  6010.                                 
  6011.                                 if (desiredType == cWord)
  6012.                                     {
  6013.                                         wordStart   = 0;
  6014.                                         wordLength  = (**theHTE).teLength;
  6015.                                         *result     = CountWords(theHTE, wordStart, wordLength);
  6016.                                     }
  6017.                                 
  6018.                                 if (desiredType == cChar)
  6019.                                     *result = (**theHTE).teLength;
  6020.                                     
  6021.                                 if (desiredType == cLine)
  6022.                                     *result = CountLines(theHTE);
  6023.                                     
  6024.                             }
  6025.                     }
  6026.                     
  6027.                 myErr = AECoerceDesc(container, typeMyText, &newDesc);
  6028.                 if (newDesc.descriptorType!=typeNull)
  6029.                     {
  6030.                         GetRawDataFromDescriptor(    &newDesc,
  6031.                                                                             (Ptr)&theTextToken,
  6032.                                                                             sizeof(theTextToken),
  6033.                                                                             &tokenSize);
  6034.                                                                          
  6035.                         ignoreErr = AEDisposeDesc(&newDesc);
  6036.                                                                          
  6037.                         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  6038.                         theHTE      = theDocument->theText;
  6039.                         
  6040.                         if (desiredType == cWord)
  6041.                             {
  6042.                                 wordStart   = theTextToken.tokenOffset-1;
  6043.                                 wordLength  = theTextToken.tokenLength;
  6044.                                 *result     = CountWords(theHTE, wordStart, wordLength);
  6045.                             }
  6046.                         
  6047.                         if (desiredType == cChar)
  6048.                             *result = theTextToken.tokenLength;
  6049.                             
  6050.                         if (desiredType == cLine)
  6051.                             *result= LineOfOffset(theHTE,theTextToken.tokenOffset-1) - 
  6052.                                              LineOfOffset(theHTE,theTextToken.tokenOffset+theTextToken.tokenLength-1)
  6053.                                              +1;                        
  6054.                     }
  6055.                     
  6056.             }
  6057.             
  6058.         return(myErr);
  6059.     }    /* MyCountProc */
  6060.  
  6061. /*******************************************************************************/
  6062. /*
  6063.     Coercion Handlers - Allow AEResolve to do the hard work
  6064. */
  6065. pascal OSErr CoerceObjToAnything(const AEDesc *theAEDesc,
  6066.                                                              DescType     toType,
  6067.                                                          long         handlerRefCon,
  6068.                                                              AEDesc       *result)
  6069. /*
  6070.     CoerceObjToAnything functions by using AEResolve to do the hard
  6071.     work.
  6072. */
  6073.     {
  6074. #pragma unused (handlerRefCon)
  6075.     
  6076.         OSErr  myErr;
  6077.         OSErr  ignoreErr;
  6078.         
  6079.         AEDesc objDesc;
  6080.             
  6081.         myErr = errAECoercionFail;
  6082.         
  6083.         result->dataHandle = nil;
  6084.         objDesc.dataHandle = nil;
  6085.         
  6086.         
  6087.         if (theAEDesc->descriptorType != typeObjectSpecifier)
  6088.             {
  6089.                 return(errAEWrongDataType);
  6090.             }
  6091.         
  6092.         /* resolve the object specifier */
  6093.         myErr = AEResolve(theAEDesc, kAEIDoMinimum, &objDesc);
  6094.             
  6095.         /* hopefully it's the right type by now, but we'll give it a nudge */
  6096.         if (myErr==noErr)
  6097.             {
  6098.                 myErr = AECoerceDesc(&objDesc, toType, result);
  6099.                 ignoreErr = AEDisposeDesc(&objDesc);
  6100.             }
  6101.             
  6102.         return(myErr);
  6103.     }    /* CoerceObjToAnything */
  6104.  
  6105.  
  6106. /* -----------------------------------------------------------------------
  6107.         Name:             InitAppleEvents
  6108.         Purpose:        Initialise the AppleEvent despatch table
  6109.      -----------------------------------------------------------------------**/
  6110.  
  6111. #pragma segment Main
  6112.  
  6113. #define noRefCon -1
  6114.  
  6115.  
  6116. pascal void InitAppleEvents(void)
  6117.     {
  6118.          OSErr aevtErr;
  6119.         
  6120.         gBigBrother    = 0;
  6121.         gCharsInBuffer = 0;
  6122.         gTypingBuffer  = (char *)NewPtr(32000);
  6123.         gTypingTargetObject.dataHandle = 0;
  6124.         
  6125.         /*set up the despatch table for the four standard apple events*/
  6126.         
  6127.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoOpenApp), noRefCon, false) ;
  6128.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(DoOpenDocument), noRefCon, false) ;
  6129.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(DoPrintDocuments), noRefCon, false) ;
  6130.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(MyQuit), noRefCon, false) ;
  6131.         
  6132.         /* set up the despatch table for the core AppleEvents for text */
  6133.         
  6134.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEDelete, NewAEEventHandlerProc(DoDeleteEdit),noRefCon, false);
  6135.  
  6136.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    NewAEEventHandlerProc(DoCutEdit),   noRefCon, false);
  6137.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   NewAEEventHandlerProc(DoCopyEdit),  noRefCon, false);
  6138.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  NewAEEventHandlerProc(DoPasteEdit), noRefCon, false);
  6139.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAESetData,NewAEEventHandlerProc(DoSetData),   noRefCon, false);
  6140.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEGetData,NewAEEventHandlerProc(DoGetData),   noRefCon, false);
  6141.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEGetDataSize,NewAEEventHandlerProc(DoGetDataSize),   noRefCon, false);
  6142.         
  6143.         aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,   NewAEEventHandlerProc(HandleNumberOfElements),   noRefCon, false);
  6144.         aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,   NewAEEventHandlerProc(DoNewElement),   noRefCon, false);
  6145.         aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,  NewAEEventHandlerProc(DoIsThereA),   noRefCon, false);
  6146.     
  6147.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEMove,   NewAEEventHandlerProc(DoMoveWindow),noRefCon, false);
  6148.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEClose,  NewAEEventHandlerProc(DoCloseWindow),noRefCon, false);
  6149.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAESave,   NewAEEventHandlerProc(DoSaveWindow),noRefCon, false);
  6150.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, NewAEEventHandlerProc(DoRevertWindow),noRefCon, false);
  6151.         
  6152.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEMakeObjectsVisible, NewAEEventHandlerProc(HandleShowSelection),   noRefCon, false);
  6153.  
  6154.         /* Now look for recording notifications */
  6155.         
  6156.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEStartedRecording, NewAEEventHandlerProc(HandleStartRecording), noRefCon, false);
  6157.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEStoppedRecording, NewAEEventHandlerProc(HandleStopRecording), noRefCon, false);
  6158.         
  6159.         /* Now Put in the required object accessors */
  6160.         
  6161.         aevtErr = AESetObjectCallbacks(nil,NewOSLCountProc(MyCountProc),nil,nil,nil,nil,nil);
  6162.         
  6163.         aevtErr = AEInstallObjectAccessor(cWindow,      typeNull,   NewOSLAccessorProc(WindowFromNullAccessor),  0,false);
  6164.         aevtErr = AEInstallObjectAccessor(cWindow,      typeMyAppl, NewOSLAccessorProc(WindowFromNullAccessor),  0,false);
  6165.                 
  6166.         aevtErr = AEInstallObjectAccessor(cApplication, typeNull,   NewOSLAccessorProc(ApplicationFromNullAccessor),  0,false);
  6167.         aevtErr = AEInstallObjectAccessor(cProperty,    typeMyAppl, NewOSLAccessorProc(PropertyFromApplAccessor),0,false);
  6168.         
  6169.         aevtErr = AEInstallObjectAccessor(cChar,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  6170.         aevtErr = AEInstallObjectAccessor(cSpot,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  6171.         aevtErr = AEInstallObjectAccessor(cWord,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  6172.         aevtErr = AEInstallObjectAccessor(cLine,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  6173.         aevtErr = AEInstallObjectAccessor(cProperty,   typeMyWndw,NewOSLAccessorProc(PropertyFromWndwAccessor),0,false);
  6174.  
  6175.    aevtErr = AEInstallObjectAccessor(cProperty,    typeMyWindowProp, NewOSLAccessorProc(PropertyFromWinPropertyAccessor),0,false);    
  6176.  
  6177.         aevtErr = AEInstallObjectAccessor(cProperty,     typeMyText,NewOSLAccessorProc(PropertyFromTextAccessor),0,false);
  6178.         aevtErr = AEInstallObjectAccessor(cChar,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  6179.         aevtErr = AEInstallObjectAccessor(cWord,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  6180.         aevtErr = AEInstallObjectAccessor(cSpot,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  6181.         aevtErr = AEInstallObjectAccessor(cLine,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  6182.         
  6183.         aevtErr = AEInstallObjectAccessor(cMenu,           typeNull,       NewOSLAccessorProc(MenuFromNullAccessor),    0,false);
  6184.         aevtErr = AEInstallObjectAccessor(cProperty,   typeMyMenu,     NewOSLAccessorProc(PropertyFromMenuAccessor),0,false);
  6185.         aevtErr = AEInstallObjectAccessor(cProperty,   typeMyMenuItem, NewOSLAccessorProc(PropertyFromMenuItemAccessor),0,false);
  6186.         aevtErr = AEInstallObjectAccessor(cMenuItem,   typeMyMenu,     NewOSLAccessorProc(MenuItemFromMenuAccessor),0,false);
  6187.         /* Now the coercion handlers */
  6188.  
  6189.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyAppl,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6190.         aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyWndw,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6191.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyText,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6192.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyTextProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6193.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyWindowProp,(AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6194.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyApplProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6195.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenu,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6196.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6197.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuItem,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6198.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyItemProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything), 0, true, false);
  6199.  
  6200.  } /* InitAppleEvents */
  6201.